题目:
给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。
返回 s 所有可能的分割方案。
示例:
输入: "aab"
输出:
[
["aa","b"],
["a","a","b"]
]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/palindrome-partitioning
方法一:
递归法:找出字符串前端的回文串,然后分割剩下的字符串,最后将分割好的字符串与首回文串结合。(递归这个过程)
class Solution:
def partition(self, s: str) -> List[List[str]]:
result = []
l = len(s)
if l==0:
return result
for i in range(l):
if self.ismatch(s[:i+1]):
X = self.partition(s[i+1:])
if X:
for Y in X:
result.append([s[:i+1]]+Y)
else:
result.append([s[:i+1]])
return result
def ismatch(self, s):
if s==s[::-1]:
return True
else:
False