Topic:
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"]]
test instructions and analysis: give a string, all the substrings are palindrome strings, and find out all possible segmentation methods.
method One : Use the Dfs method to find, each time find the string header possible palindrome, and then cut off, continue to find the remaining string s, if the last S.length ==0, then the description string can be all separated into palindrome string, satisfies the condition. Added to the result set.
Code:
classSolution { PublicList<list<string>>partition (String s) {List<List<String>> res =NewArraylist<>(); if(s==NULL|| S.length () ==0)returnRes; //dfs?? Split out every palindromeDFS (Res,NewArraylist<string>(), s); returnRes; } Private voidDFS (list<list<string>> Res,list<string>arraylist,string s) { if(s.length () = = 0) {Res.add (NewArraylist<>(arrayList)); return; } for(inti = 0;i<s.length (); i++) {String str= S.substring (0,i+1); if(Ispalindrome (str)) {arraylist.add (str); DFS (Res,arraylist,s.substring (Str.length ())); Arraylist.remove (Arraylist.size ()-1); } } } Private BooleanIspalindrome (String s) {//determine if S is a palindrome for(inti = 0;i<s.length ()/2;i++){ if(S.charat (i)! = S.charat (s.length () -1-i))return false; } return true; }}
[Leetcode] 131. Palindrome Partitioning Java