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"]]
Recursive solution, no difficulty.
Public classSolution {List List=NewArraylist<arraylist<string>>(); Char[] word; String SS; String[] result; PublicList<list<string>>partition (String s) {intLen =s.length (); if(len = = 0) returnlist; if(len = = 1) {ArrayList<String> L1 =NewArraylist<string>(); L1.add (s); List.add (L1); returnlist; } SS=s; Word=S.tochararray (); Result=NewString[len]; for(inti = 0;i < len;i++){ if(Ispalindrome (0, i)) {result[0] = s.substring (0,i+1); Helper (i+1,1); } } returnlist; } Public voidHelperintStartintnum) { if(Start = =word.length) {ArrayList ll=NewArraylist<string>(); for(inti = 0;i<num;i++) Ll.add (Result[i]); List.add (LL); return ; } for(inti = start; I < word.length;i++){ if(Ispalindrome (start,i)) {Result[num]= Ss.substring (start,i+1); Helper (i+1,num+1); } } } Public BooleanIspalindrome (intStartintend) { while(Start <end) { if(Word[start] = =Word[end]) {Start++; End--; }Else return false; } return true; }}
Leetcode 131. Palindrome Partitioning-----java