Return all possible palindrome partitioning of s.
[
["aa","b"],
["a","a","b"]
]
public List<List<String>> partition(String s) {
List<List<String>> res = new ArrayList<>();
if (s == null || s.length() == 0) {
return res;
}
List<String> tmp = new ArrayList<>();
dfs(res, tmp, s);
return res;
}
private void dfs(List<List<String>> res, List<String> tmp, String s) {
if (s.length() == 0) {
res.add(new ArrayList<>(tmp));
return;
}
for (int i = 1; i <= s.length(); i++) {
String part = s.substring(0, i);
if (!isPar(part)) {
continue;
}
tmp.add(part);
dfs(res, tmp, s.substring(i));
tmp.remove(tmp.size() - 1);
}
}
private boolean isPar(String s) {
int len = s.length();
for (int i = 0; i < len / 2; i++) {
if (s.charAt(i) != s.charAt(len - i - 1)) {
return false;
}
}
return true;
}
public List<List<String>> partition(String s) {
List<List<String>> res = new ArrayList<>();
if (s == null || s.length() == 0) {
return res;
}
List<String> tmp = new ArrayList<>();
if (s.length() == 1) {
tmp = new ArrayList<>();
tmp.add(s);
res.add(tmp);
return res;
}
searchRest(s, tmp, res, 0);
return res;
}
private boolean isPalindrom(String s) {
for (int i = 0; i < s.length() / 2; i++) {
if (s.charAt(i) != s.charAt(s.length() - i - 1)) {
return false;
}
}
return true;
}
private void searchRest(String s, List<String> tmp, List<List<String>> res, int start) {
if (s.length() == start) {
res.add(new ArrayList<>(tmp));
return;
}
for (int i = start; i < s.length(); i++) {
String cur = s.substring(start, i + 1);
if (isPalindrom(cur)) {
tmp.add(cur);
searchRest(s, tmp, res, start + cur.length());
tmp.remove(tmp.size() - 1);
}
}
}