leetcode131 分割回文串 python

分割回文串(中等)(leetcode131)

给一个字符串s,将s分割成一些子串,使每个子串都是回文串,返回 s 所有可能的分割方案。

示例: 输入: "aab"  输出:

[

  ["aa","b"],

  ["a","a","b"]

]

def partition(self, s):

res=[]

        if(s==''):

            return []

        if(s==s[::-1]):

            res.append([s])

        for i in range(len(s)):

            if(s[:i+1]==s[i::-1]):

                ss=self.partition(s[i+1:])

                for c in ss:

                    if(c!=[]):

                        res.append([s[:i+1]]+c)

        return res

猜你喜欢

转载自blog.csdn.net/qq_37792144/article/details/89374712