Return all possible palindrome partitioning of s.
[
["aa","b"],
["a","a","b"]
]
public class Solution {
/*
* @param s: A string
* @return: A list of lists of string
*/
public List<List<String>> partition(String s) {
// write your code here
List<List<String>> res = new ArrayList<>();
List<String> partition = new ArrayList<>();
if (s == null || s.length() == 0) {
return res;
}
//helper(s, partition, res, 0);
dfs(s, partition, res);
return res;
}
private void dfs(String s,
List<String> partition,
List<List<String>> res) {
if (s.equals("")) {
res.add(new ArrayList<String>(partition));
}
for (int i = 0; i < s.length(); i++) {
String substring = s.substring(0, i + 1);
if (!isPalindrome(substring)) {
continue;
}
partition.add(substring);
dfs(s.substring(i + 1), partition, res);
partition.remove(partition.size() - 1);
}
}
private void helper(String s,
List<String> partition,
List<List<String>> res,
int start) {
if (start == s.length()) {
res.add(new ArrayList<String>(partition));
}
for (int i = start; i < s.length(); i++) {
String substring = s.substring(start, i + 1);
if (!isPalindrome(substring)) {
continue;
}
partition.add(substring);
helper(s, partition, res, i + 1);
partition.remove(partition.size() - 1);
}
}
private boolean isPalindrome(String s) {
for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
if (s.charAt(i) != s.charAt(j)) {
return false;
}
}
return true;
}
}