- 23.7%

https://leetcode.com/problems/palindrome-partitioning-ii/#/description

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.

1 | For example, given s = "aab", |

13ms, September 22, 2016

My solution does not need a table for palindrome, is it right ? It uses only O(n) space.

1 | class Solution { |

https://discuss.leetcode.com/topic/2048/my-dp-solution-explanation-and-code

My DP Solution ( explanation and code)

Calculate and maintain 2 DP states:

- pal[i][j] , which is whether s[i..j] forms a pal
- d[i], which is the minCut for s[i..n-1]

Once we comes to a pal[i][j]==true:

- if j==n-1, the string s[i..n-1] is a Pal, minCut is 0, d[i]=0;
- else: the current cut num (first cut s[i..j] and then cut the rest s[j+1…n-1]) is 1+d[j+1], compare it to the exisiting minCut num d[i], repalce if smaller.

d[0] is the answer.

1 | class Solution { |

Two C++ versions given (one DP 28ms, one Manancher-like algorithm 10 ms)

One typical solution is DP based. Such solution first constructs a two-dimensional bool array isPalin to indicate whether the sub-string s[i..j] is palindrome. To get such array, we need O(N^2) time complexity. Moreover, to get the minimum cuts, we need another array minCuts to do DP and minCuts[i] saves the minimum cuts found for the sub-string s[0..i-1]. minCuts[i] is initialized to i-1, which is the maximum cuts needed (cuts the string into one-letter characters) and minCuts[0] initially sets to -1, which is needed in the case that s[0..i-1] is a palindrome. When we construct isPalin array, we update minCuts everytime we found a palindrome sub-string, i.e. if s[i..j] is a palindrome, then minCuts[j+1] will be updated to the minimum of the current minCuts[j+1] and minCut[i]+1(i.e. cut s[0..j] into s[0,i-1] and s[i,j]). At last, we return minCuts[N].

So the complexity is O(N^2). However, it can be further improved since as described above, we only update minCuts when we find a palindrome substring, while the DP algorithm spends lots of time to calculate isPalin, most of which is false (i.e. not a palindrome substring). If we can reduce such unnecessary calculation, then we can speed up the algorithm. This can be achieved with a Manancher-like solution, which is also given as following.

1 | // DP solution |

The Manancher-like solution scan the array from left to right (for i loop) and only check those sub-strings centered at s[i]; once a non-palindrome string is found, it will stop and move to i+1. Same as the DP solution, minCUTS[i] is used to save the minimum cuts for s[0:i-1]. For each i, we do two for loops (for j loop) to check if the substrings s[i-j .. i+j] (odd-length substring) and s[i-j-1.. i+j] (even-length substring) are palindrome. By increasing j from 0, we can find all the palindrome sub-strings centered at i and update minCUTS accordingly. Once we meet one non-palindrome sub-string, we stop for-j loop since we know there no further palindrome substring centered at i. This helps us avoid unnecessary palindrome substring checks, as we did in the DP algorithm. Therefore, this version is faster.

1 | //Manancher-like solution |

https://discuss.leetcode.com/topic/22388/56-ms-python-with-explanation

56 ms python with explanation

Algorithm (460 ms) credits go to:

https://leetcode.com/discuss/9476/solution-does-not-need-table-palindrome-right-uses-only-space

The main algorithm idea is if s[i,j] is a palindrome, then the minCut(s[:j]) is at most minCut(s[:i-1])+1. This literally needs to find out all possible palindromes in the list. The above post provides an efficient search algorithm. O(n) space and O(n^2) time complexity.

Further acceleration (460 ms -> 56 ms) credits go to:

https://leetcode.com/discuss/43950/python-100ms-extra-dealing-super-cases-reduces-576ms-100ms

The main idea for acceleration is to quickly check and exclude a few long palindrome tests..

1 | def minCut(self, s): |

The following code simply implements the algorithm without any optimization (1800 ms), and should be easier to understand. O(n) space and O(n^3) time complexity.

1 | def minCut(self, s): |

https://discuss.leetcode.com/topic/32575/easiest-java-dp-solution-97-36

Easiest Java DP Solution (97.36%)

This can be solved by two points:

- cut[i] is the minimum of cut[j - 1] + 1 (j <= i), if [j, i] is palindrome.
- If [j, i] is palindrome, [j + 1, i - 1] is palindrome, and c[j] == c[i].

The 2nd point reminds us of using dp (caching).

1 | a b a | c c |

Hope it helps!

1 | public int minCut(String s) { |