给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。
返回 s 所有可能的分割方案。
示例:
输入: "aab" 输出: [ ["aa","b"], ["a","a","b"] ]
class Solution(object):
def partition(self, s):
"""
:type s: str
:rtype: List[List[str]]
"""
self.res = []
self.temp = []
def sub_partition(string):
if string == string[::-1]: #到了尽头,加入到结果
self.temp.append(string)
self.res.append(copy.copy(self.temp)) #浅拷贝
self.temp.pop()
for i in range(1,len(string)):
a = string[:i]
if a == a[::-1]:
self.temp.append(a)
sub_partition(string[i:])
self.temp.pop()
sub_partition(s)
return self.res
执行用时: 112 ms, 在Palindrome Partitioning的Python提交中击败了91.78% 的用户