python
class Solution:
def partition(self, s: str) -> List[List[str]]:
# 思路是回溯
n=len(s)
res=[]
path=[]
def backtrack(i):
if i == n:
res.append(path[:])
return
for j in range(i,n):
t=s[i:j+1]
if t==t[::-1]:
path.append(t)
backtrack(j+1)
path.pop()
backtrack(0)
return res
思路是:
- 递归加回溯