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.
DP: mincut[i, j] = 0, if the substring between i and j is a palindrome. Otherwise, mincut[i, j] = min (1 + mincut[i, k] + mincut[k+1, j]), i <= k < j.

We need a two-dimensional array storing whether the substring between i and j is a palindrome.
We assume that chk[i] is the minimum cuts needed for a palindrome partitioning of the substring between i and s.end(); essentially, chk[i] = min(1 + chk[j + 1]), if the substring in [i, j] is a palindrome, i <= j < s.end().

The tricky part is that we don't need to obtain the entire isP matrix. One-pass scan is enough for this problem.

Comments

Popular posts from this blog

Maximum Gap

[ITint5] Maximum Subarray for a Circular Array

[CC150] Chapter 4 Trees and Graphs