Given A string s, partition s such that every substring of the partition are a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab"
,
Return
[ ["AA", "B"], ["A", "a", "B"] ]
Public classSolution {//Dfs idea, the Getpart function means starting at the start index of S to find a palindrome, the DFS process is, take the start of the I character, if it is a palindrome, save the results, from the start+i began to traverseList<list<string>>Res; List<String>seq; PublicList<list<string>>partition (String s) {res=NewArraylist<list<string>>(); Seq=NewArraylist<string>(); GetPart (s),0); returnRes; } Public voidGetPart (String S,intstart) { if(start==s.length ()) {Res.add (NewArraylist<string>(seq)); } for(intI=start+1;i<=s.length (); i++){ if(Ispart (s.substring (start,i))) {Seq.add (s.substring (start,i)); GetPart (S,i); Seq.remove (Seq.size ()-1); } } } Public BooleanIspart (String s) {intI=0; intJ=s.length ()-1; while(i<j) { if(S.charat (i)!=s.charat (j))return false; I++; J--; } return true; } }
[Leedcode 131] Palindrome Partitioning