[leetcode]Palindrome Partitioning @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning/
题意:
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example,
given s = "aab"
,
Return
[ ["aa","b"], ["a","a","b"] ]
解题思路:回文的分割问题。同样是需要穷举出所有符合条件的集合,那么我们还是使用dfs。
代码:
class Solution: # @param s, a string # @return a list of lists of string def isPalindrome(self, s): for i in range(len(s)): if s[i] != s[len(s)-1-i]: return False return True def dfs(self, s, stringlist): if len(s) == 0: Solution.res.append(stringlist) for i in range(1, len(s)+1): if self.isPalindrome(s[:i]): self.dfs(s[i:], stringlist+[s[:i]]) def partition(self, s): Solution.res = [] self.dfs(s, []) return Solution.res
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。