来源LeetCode 131:

题目:


给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。

返回 s 所有可能的分割方案。

示例:

输入: "aab"
输出:
[
  ["aa","b"],
  ["a","a","b"]
]

解题代码:

 1 class Solution:
 2     def partition(self, s):
 3         """
 4         :type s: str
 5         :rtype: List[List[str]]
 6         """
 7         res=[]
 8         self.dfs(s,[],res)
 9         return res
10     
11     def dfs(self,s,path,res):
12         if not s:
13             res.append(path)
14             return
15         for i in range(1,len(s)+1):
16             if s[:i]==s[i-1::-1]:
17                 self.dfs(s[i:],path+[s[:i]],res)
18                 

 

相关文章:

  • 2022-12-23
  • 2021-11-10
  • 2021-09-20
  • 2021-07-02
  • 2021-12-23
  • 2022-01-10
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-13
  • 2021-07-11
  • 2021-10-10
  • 2021-10-26
  • 2021-10-12
相关资源
相似解决方案