首先看Leetcode上的Palindrome Partitioning题目:
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"] ]
一开始采用的是递归解法:
public class Solution{
List<List<String>> result = new ArrayList<List<String>>();
public List<List<String>> partition(String s) {
helper(s, new ArrayList<String>());
return result;
}
private void helper(String s, List<String> cur){ //DFS every combinations
if(s.length() == 0){result.add(cur); return;}
for(int i = 1; i <= s.length(); i++){
String sub = s.substring(0,i);
if(isPalindromeString(sub)){
List<String> newList = new ArrayList<String>(cur);
newList.add(sub);
helper(s.substring(i,s.length()), newList);
}
else continue; //not palindrome, ignore it
}
}
}
时间复杂度比较高,因为每个子串都是重新递归,没有保存中间结果。利用动态规划对算法做改进,如下:
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];
}
}
其中,pair存储子串(i到j)是否已经是回文串,resul[n]t存储前n个字符的临时结果。从之前的结果我们可以得到现在的结果。
再来看看Palindrome Partitioning ii:
Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s = "aab"
,
Return 1
since the palindrome partitioning ["aa","b"]
could be produced using 1 cut.
类似地,利用上面的动态规划方法,解法如下:
public class Solution{
public int minCut(String s) {
int len = s.length();
boolean[][] pair = new boolean[len][len];
int[] result = new int[len+1];
result[0] = 0;
for(int i=0; i<len; i++){
result[i+1] = Integer.MAX_VALUE;
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;
if(left == 0) result[i+1] = 0;
else{
result[i+1] = Math.min(result[left] + 1, result[i+1]);
}
}
}
}
return result[len];
}
}
然而此题并不要求给出所有划分的情况,可以考虑减少空间复杂度至O(n)。有一种很巧妙的解法:
这里,只开辟O(n)的空间存储前n个字符的最少划分次数,然后之后到底有多少个回文串,例如,遍历到字符‘b’时,
s[i] = ‘b’, 且 s[i-1,i+1]是回文 “aba”,如下:
.......aba...
|<-X->| ^
|<---Y-->|
我们知道s[0,i-1)最小划分数是 X, 所以s[0,i+1] 即Y 的最小划分数不会大于X+1. 所以我们需要找到s[0,i+1]中所有的回文串,以找到最小的划分数。代码如下:
class Solution {
public:
int minCut(string s) {
int n = s.size();
vector<int> cut(n+1, 0); // number of cuts for the first k characters
for (int i = 0; i <= n; i++) cut[i] = i-1;
for (int i = 0; i < n; i++) {
for (int j = 0; i-j >= 0 && i+j < n && s[i-j]==s[i+j] ; j++) // odd length palindrome
cut[i+j+1] = min(cut[i+j+1],1+cut[i-j]);
for (int j = 1; i-j+1 >= 0 && i+j < n && s[i-j+1] == s[i+j]; j++) // even length palindrome
cut[i+j+1] = min(cut[i+j+1],1+cut[i-j+1]);
}
return cut[n];
}
};