## Monday, November 23, 2015

### Chunked Palindrome - Google Interview

voabcvo的话就是算3个了，分法就是按照你的说的一样：(vo)(abc)(vo)

(vo)(l)(vo)(l)的话就不能这么分了，因为他不是回文，正确的划分为（volvol），函数返回1.
(vo)(l)(vo)(l)(vo) 就是这么分的，函数返回5。

http://www.cnblogs.com/EdwardLiu/p/5136947.html

4     public int countChunk(String str) {
5         if (str==null || str.length()==0) return 0;
6         int sum = 0;
7         int l=0, r=str.length()-1;
8         int preL = l, preR = r;
9         while (l < r) {
10             String left = str.substring(preL, l+1);
11             String right = str.substring(r, preR+1);
12             if (left.equals(right)) {
13                 preL = l+1;
14                 preR = r-1;
15                 sum += 2;
16             }
17             l++;
18             r--;
19         }
20         if (preL <= preR) sum+=1;
21         return sum;
22     }
http://www.geeksforgeeks.org/longest-possible-chunked-palindrome/
The entire idea is to create chunks from left and right and recursively.

As you can see, we can match substring from left side chunck and match it with the exact right side chunk. Once we get a match, we recursively count the length of the longest possible chunked palindrome in the remaining string. We end the recursion once no string is left or when no more valid chunked parts can be found.
private static int LPCRec(String curr_str, int count,
int len, String str)
{
// if there is noting at all!!
if (curr_str == null || curr_str.isEmpty())
return (0);

// if a single letter is left out
if (curr_str.length() <= 1)
{
if (count != 0 && str.length() - len <= 1)
return (count + 1);
else
return (1);
}

// for each length of substring chunk in string
int n = curr_str.length();
for (int i = 0; i < n/2; i++)
{
// if left side chunk and right side chunk
// are same
if (curr_str.substring(0, i + 1).
equals(curr_str.substring(n-1-i, n)))
{
// Call LCP for the part between the
// chunks and add 2 to the result.
// Length of string evaluated till
// now is increased by (i+1)*2
return LPCRec(curr_str.substring(i+1, n-1-i),
count + 2,
len + (i+1)*2, str);
}
}

return count + 1;
}

// Wrapper over LPCRec()
public static int LPC(String str)
{
return LPCRec(str, 0, 0, str);
}

public static  int chunkNum(String s){
if(s == null || s.length() == 0){
return 0;
}
int length = s.length();
int[][] DP = new int[length][length];
for(int i=length-1;i>=0;i--){
for(int j=i;j<length;j++){
if(i == j){
DP[i][j] = 1;
}
else{
int sum = 0;
int mid = i + (j-i) / 2;
for(int count=i;count<=mid;count++){
String pre = s.substring(i,count+1);
String post = s.substring(j-count+i,j+1);
if(pre.equals(post)){
sum += DP[count+1][j-count+i-1];
}
}
DP[i][j] = sum;
}
}
}
return DP[0][length-1];
}

int countMaxChunks(string str) {
int count = 0;
int i = 0, j = str.size()-1; // scan from the two sides
int prev_i = i, prev_j = j;
while(i<j) {
string chunk1 = str.substr(prev_i, i+1);
string chunk2 = str.substr(j, prev_j+1);
if (chunk1==chunk2) {
count++;
prev_i = i+1;
prev_j = j-1;
}
i++;
j--;
}
count *= 2;
// if odd string
if (i==j)
count++;
// even string and still has chunk left
else if (prev_i<prev_j)
count++;
return count;
}