题目链接:https://leetcode.com/problems/palindrome-partitioning/
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
Example:
Input: "aab" Output: [ ["aa","b"], ["a","a","b"] ]
class Solution {
public List<List<String>> partition(String s) {
List<List<String>> ans=new ArrayList();
if(s==null||s.length()==0)
return ans;
helper(ans,new ArrayList<String>(),s,0);
return ans;
}
public void helper(List<List<String>> ans,List<String> list,String s,int pos){
if(pos==s.length()){
ans.add(new ArrayList(list));
return;
}
for(int i=pos;i<s.length();i++){
if(!isPalindrome(s.substring(pos,i+1)))
continue;
list.add(s.substring(pos,i+1));
helper(ans,list,s,i+1);
list.remove(list.size()-1);
}
}
public boolean isPalindrome(String s){
if(s.length()==0)
return false;
if(s.length()==1)
return true;
int i=0,j=s.length()-1;
while(i<j){
if(s.charAt(i)!=s.charAt(j))
return false;
i++;
j--;
}
return true;
}
}