Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab"
,
Return
[ ["aa","b"], ["a","a","b"] ]
Subscribe to see which companies asked this question
经典的回溯方法,回溯基本上就是这个方法,按照这个模子套用就行。
class Solution(object):
def bt(self,s,temp,start,l,res):
#print l,temp,'@@'
if l == len(s):
res.append(temp)
return
for i in range(start+1,len(s)+1):
t = s[start:i]
#print i,t
if t == t[::-1]:
self.bt(s,temp+[t],i,l+(i-start),res)
def partition(self, s):
res = []
self.bt(s,[],0,0,res)
#print res
return res