131. 分割回文串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution:
def partition(self, s: str) -> List[List[str]]:
path,result = [],[]
def symmetric_str(path) -> bool: # 判断是否为回文串;
is_or_not = True
for num in path:
left,right = 0,len(num)-1
while left<=right and num[left]==num[right]:
left += 1
right -= 1
if left <= right:
is_or_not = is_or_not and False
return is_or_not
def split_tree(s,start_index): # 用 分叉树的回溯,解决 组合、分割。
if start_index>=len(s):
if symmetric_str(path):
result.append(path.copy())
return
for i in range(start_index,len(s)):
path.append(s[start_index:i+1])
split_tree(s,i+1)
path.pop()
return result
return split_tree(s,0)