base case为开始索引startIndex 大于等于字符串长度,则找到一组答案
class Solution:
def partition(self, s: str) -> List[List[str]]:
path = []
paths = []
def backtrack(s, startIndex):
if startIndex >= len(s):
paths.append(path[:])
return
for i in range(startIndex, len(s)):
if is_palindrome(s, startIndex, i):
path.append(s[startIndex:i+1])
backtrack(s, i+1)
path.pop()
else:
continue
def is_palindrome(s, start, end):
while start < end:
if s[start] != s[end]:
return False
start += 1
end -= 1
return True
backtrack(s, 0)
return paths