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"] ]
public class Solution { //DFS思想,getPart函数的意思是从s的start索引处开始求回文,DFS过程是,取start的i个字符,如果是回文,保存结果,从start+i开始遍历 List<List<String>> res; List<String> seq; public List<List<String>> partition(String s) { res=new ArrayList<List<String>>(); seq=new ArrayList<String>(); getPart(s,0); return res; } public void getPart(String s,int start){ if(start==s.length()){ res.add(new ArrayList<String>(seq)); } for(int i=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 boolean isPart(String s){ int i=0; int j=s.length()-1; while(i<j){ if(s.charAt(i)!=s.charAt(j))return false; i++; j--; } return true; } }