|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
Example:
Input: "aab"
Output:
[
["aa","b"],
["a","a","b"]
]
- class Solution:
- def partition(self, s: str) -> List[List[str]]:
- results = []
-
- def palindrome(left, right):
- while left < right:
- if s[left] != s[right]:
- return False
- left += 1
- right -= 1
- return True
-
- def dfs(start, result):
- if start >= len(s):
- results.append(result[:])
- return
-
- for i in range(start, len(s)):
- if palindrome(start, i):
- result.append(s[start: i + 1])
- dfs(i + 1, result)
- result.pop(len(result) - 1)
-
- dfs(0, [])
- return results
复制代码 |
|