It is easy to find out that it's a dynamic programming problem. Certainly we can use recursion to solve it, but we will see that there're a lot of repeated computations, and therefore recursion is inefficient in this kind of situation.

DP approach isn't difficult, a good and brief explanation can be found here.

Let dp[i] = min cut for substring [i...n], where n is the length of input string.

Then dp[i] = 0, if substring[i..n] itself is a palindrome

or dp[i] = 1+dp[j], if substring(i, j) is a palindrome ( i<j<n )

Here comes the interesting part, how do we know if substring(i, j) is a palindrome or not ?

If we check it directly by going though characters from i to j-1, it reveals a overall runtime of O(n^3) of this algorithm. The trick is using another DP:

substring(i, j) is palindrome if charAt(i) == charAt(j-1) && subtring(i+1,j-1) is palindrome.

So, this is a interesting mixed DP problem and can be solved in O(n^2).

Note that this post made a good point that it can be regarded as a variant of Matrix Chain Multiplication Problem. They gave a neat code for this problem using 2D dynamic programming algorithm which is very similar with the one to solve Matrix Chain Multiplication Problem. It makes perfect sense, but the time complexity is O(n^3).

As I talked it before, this problem can be solved in runtime of O(n^2). So what's the difference between it and Matrix Chain Multiplication Problem? Well, by using anoter DP, we can find out if substring(i, j) is palindrome in O(1) time while we can not directly get the min multiplications of sub matrices(i,j) if we applied the same strategy to it.

Another related problem is problem 17.14 in the book

*Cracking the Coding Interview:*

Given a text without spaces,

*e.g."helloworldniceday",*

Find the minimal words it can be divided into.

*e.g. "hello world nice day" -> 4 words.*

It occurs to me that this is another variant of Matrix Chain Multiplication Problem with 2-D DP at first, but actually it's much easier than I thought. 1-D DP is enough, just like the Palindrome Partition problem I talked first.

//-1 means this text can't be divided to valid words. public static int getMinSeperateWords(String text){ int len = text.length(); int[] dp = new int[len]; for(int i=len-1;i>=0;i--){ if(Dict.isValid(text.substring(i))){ dp[i]=1; continue; } int min=Integer.MAX_VALUE; for(int j=i+1;j<len;j++){ if(dp[j]!=-1 && Dict.isValid(text.substring(i,j))){ if(dp[j]+1<min) min=dp[j]+1; } } dp[i]= min==Integer.MAX_VALUE ? -1:min; } return dp[0]; }In conclusion, problems can be related while they can also be harder or easier than the similar ones. Practice makes perfect.

## No comments:

## Post a Comment