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"] ]
Decomposes a given string so that each division is a palindrome structure requires all possible combinations to be given
A typical backtracking solution starts at the beginning of the i= (Index,s.length ()-1) If the index-i bit satisfies the palindrome structure, the continuation of recursion from the i+1 bit needs to be noted that the end condition can be index=s.length () that all bits are split. The code is as follows:
public class Solution { public list<list<string>> partition (String s) {list<list<string >> res=new arraylist<list<string>> (); List<string> tmp=new arraylist<string> ();p Art (res, S, tmp, 0); return res; } Public void part (list<list<string>> res,string s,list<string> tmp,int index) {if (index== S.length ()) {res.add (TMP); return;} for (int i=index;i<s.length (); i++) {if (Ispalindrome (S, index, i)) {list<string> ntmp=new arraylist< String> (); Ntmp.addall (TMP), Ntmp.add (s.substring (index,i+1));p Art (res, S, ntmp, i+1);}} Public Boolean ispalindrome (String s,int Start,int end) {for (int i=0;i< (end-start+1)/2;i++) {if (S.charat (i+ Start)!=s.charat (end-i)) {return false;}} return true;} }
Java-palindrome Partitioning