class Solution:
def partition(self, s: str) -> List[List[str]]:
path = []
res = []
self.dfs(path, res, s, start=0)
return res
def dfs(self, path, res, s, start):
if start >= len(s):
res.append(path.copy())
return
for i in range(start, len(s)): # 注意范围
if self.is_valid(s, start=start, end=i): # 左闭右闭
path.append(s[start:(i+1)]) # 左闭右开
self.dfs(path, res, s, i+1)
path.pop()
def is_valid(self, string, start, end):
l = start
r = end
while r >= l:
if string[r] == string[l]:
r -= 1
l += 1
else:
return False
return True
class Solution:
def partition(self, s: str) -> List[List[str]]:
path = []
res = []
self.dfs(s, path, res, start=0)
return res
def dfs(self, s, path, res, start):
if start >= len(s):
res.append(path[:])
return
for i in range(start, len(s)):
temp = s[start:i+1]
if temp == temp[::-1]:
path.append(temp)
self.dfs(s, path, res, i+1)
path.pop()
else:
continue