题目描述

题解
class Solution {
public List<List<String>> partition(String s) {
List<List<String>> ret = new ArrayList<>();
if(s.length()==0)
return ret;
List<String> temp = new ArrayList<>();
findPatition(temp,s,ret,0);
return ret;
}
private void findPatition(List<String> temp, String s, List<List<String>> ret, int i)
{
int n = s.length();
if(String.join("",temp).length()==s.length()) {
System.out.println(temp);
ret.add(new ArrayList<>(temp));
System.out.println(ret);
return;
}
for(int j = i+1; j<=n;j++)
{
if(isHuiwen(s.substring(i,j))) {
temp.add(s.substring(i,j));
findPatition(temp,s,ret,j);
temp.remove(temp.size()-1);
}
}
}
private boolean isHuiwen(String strr)
{
int i = 0;
int j = strr.length()-1;
while(i<=j)
{
if(strr.charAt(i)!=strr.charAt(j))
return false;
i++;
j--;
}
return true;
}
}