Source of the topic:
https://leetcode.com/problems/palindrome-partitioning/
Test Instructions Analysis:
Given a string s, the S is split into substrings so that all substrings are palindrome strings, returning all such sets of substrings. For example s = "AaB", then return [["AA", "B"],["A", "a", "B"]].
Topic Ideas:
This is a dynamic programming problem, if S[:I] is a palindrome string, then s[:i] x Solve (s[i+1:]), (x is the Cartesian product, solve (s[i+1:]) is the answer to S[i+1:]). So only need to judge S[:i] is not a palindrome can be.
Code (Python):
1 classsolution (object):2 defpartition (self, s):3 """4 : Type S:str5 : Rtype:list[list[str]]6 """7m =Len (s)8 ifm = =0:9 return []Ten defISP (I,J): One whileI <J: A ifS[i]! =S[j]: - returnFalse -i + = 1 theJ-= 1 - returnTrue - defSolve (i): -Ans = [] + ifi = = M-1: - return[[S[i] ] +j =I A whileJ <m: at ifISP (I,J): -TMP = Solve (j + 1) - ifLen (tmp) = =0: -Ans.append ([S[i:j + 1]]) - Else: - forKinchtmp: inAns.append ([S[i:j + 1]] +k) -J + = 1 to returnans + returnSolve (0) - the
View Code
[Leetcode] (python): 131-palindrome Partitioning