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>> res = new ArrayList<>();
if(s!=null && !s.isEmpty()) {
set(res,new ArrayList<>(),s,0);
}
return res;
}
public void set (List<List<String>> res,List<String> tmp,String s,int start) {
if(start >= s.length()) {
if(!tmp.isEmpty()) {
res.add(new ArrayList<>(tmp));
}
return ;
}
for(int i=start+1;i<=s.length();i++) {
String tmpValue = s.substring(start, i);
if (val(tmpValue)) {
tmp.add(tmpValue);
set(res, tmp, s, i);
tmp.remove(tmp.size() - 1);
} else {
continue;
}
}
}
public boolean val(String value) {
StringBuffer tmp = new StringBuffer(value);
if(tmp.reverse().toString().equals(value)) {
return true;
}
return false;
}
}