1 问题
给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是
回文串。返回 s 所有可能的分割方案。
示例 1:
输入:s = “aab”
输出:[[“a”,“a”,“b”],[“aa”,“b”]]
示例 2:
输入:s = “a”
输出:[[“a”]]
2 答案
- 方法一:输入的视角(逗号选或不选)
class Solution:
def partition(self, s: str) -> List[List[str]]:
ans = []
path = []
n = len(s)
def dfs(i, start):
if i == n:
ans.append(path[:])
return
if i < n - 1:
dfs(i+1, start)
t = s[start: i+1]
if t == t[::-1]:
path.append(t)
dfs(i+1, i+1)
path.pop()
dfs(0, 0)
return ans
- 方法二:答案的视角(枚举子串结束位置)
class Solution:
def partition(self, s: str) -> List[List[str]]:
ans = []
path = []
n = len(s)
def dfs(i):
if i == n:
ans.append(path.copy())
return
for j in range(i, n):
t = s[i: j+1]
if t == t[::-1]:
path.append(t)
dfs(j+1)
path.pop()
dfs(0)
return ans
https://leetcode.cn/problems/palindrome-partitioning/solutions/2059414/hui-su-bu-hui-xie-tao-lu-zai-ci-pythonja-fues