1. Problem
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab"
,
Return
[
["aa","b"],
["a","a","b"]
]
2. Solution
2.1. BackTracking DFS Thought
利用backTracking()的方法, 不断进行回溯处理。并且进行DFS处理
public class Solution {
public List<List<String>> partition(String s) {
List<List<String>> res = new ArrayList<List<String>>();
List<String> list = new ArrayList<String>();
dfs(s,0,list,res);
return res;
}
public void dfs(String s, int pos, List<String> list, List<List<String>> res){
if(pos==s.length()) res.add(new ArrayList<String>(list));
else{
for(int i=pos;i<s.length();i++){
if(isPal(s,pos,i)){
list.add(s.substring(pos,i+1));
dfs(s,i+1,list,res);
list.remove(list.size()-1);
}
}
}
}
public boolean isPal(String s, int low, int high){
while(low<high) if(s.charAt(low++)!=s.charAt(high--)) return false;
return true;
}
}
2.2. DP Solution
public class Solution {
public static List<List<String>> partition(String s) {
int len = s.length();
List<List<String>>[] result = new List[len + 1];
result[0] = new ArrayList<List<String>>();
result[0].add(new ArrayList<String>());
boolean[][] pair = new boolean[len][len];
for (int i = 0; i < s.length(); i++) {
result[i + 1] = new ArrayList<List<String>>();
for (int left = 0; left <= i; left++) {
if (s.charAt(left) == s.charAt(i) && (i-left <= 1 || pair[left + 1][i - 1])) {
pair[left][i] = true;
String str = s.substring(left, i + 1);
for (List<String> r : result[left]) {
List<String> ri = new ArrayList<String>(r);
ri.add(str);
result[i + 1].add(ri);
}
}
}
}
return result[len];
}
}
Here the pair is to mark a range for the substring is a Pal. if pair[i][j] is true, that means sub string from i to j is pal.
The result[i], is to store from beginng until current index i (Non inclusive), all possible partitions. From the past result we can determine current result.