Palindrome Partitioning II
Palindrome Partitioning II is LeetCode 132 (Hard). Cut the string s into pieces that are all palindromes, using as few cuts as possible, and return the number of cuts.
- The answer counts cuts, which is one less than the number of pieces. A string that already reads the same backwards needs 0.
scan have 2,000 letters, so trying every way to cut it is out of reach. An O(n²) method is the target.
- 1 <= s.length <= 2000
- s consists of lowercase English letters only.
Intuition
Answer a smaller question for every prefix: how few cuts does s[0..j] need? Call it cuts[j].
The best split of s[0..j] ends with some palindrome s[i..j], and the part before it, s[0..i-1], is itself split as cheaply as possible. So cuts[j] is the smallest cuts[i - 1] + 1 over all palindromes s[i..j], or 0 if the whole prefix is a palindrome.
- Palindrome checks in the same loop:
s[i..j]is a palindrome when its end letters match ands[i+1..j-1]is one. That inner piece ends atj - 1, a column already filled, so the check is a table lookup.
"Minimum number of cuts" or "fewest pieces" with a rule on each piece is a prefix DP: the best answer for a prefix is the best answer for a shorter prefix plus one more piece.
Approach
Before reading on, take "abaab" and ask: if the last piece is "baab", what is left to solve, and has that already been solved? Aim for O(n²) time.
Start each prefix at its worst case
For end j, set best = j. Cutting between every pair of neighbouring letters always works and uses exactly j cuts, so the real answer can only be lower.
Try every possible last piece
Loop i from 0 to j. The piece s[i..j] is a palindrome when s[i] == s[j] and the inside is short (under two letters apart) or already marked in pal[i + 1][j - 1]. Record the result in pal[i][j] for later columns.
Price a palindromic last piece
If i is 0, the whole prefix is one palindrome and needs no cut, so best becomes 0. Otherwise one cut goes just before i, and the part before it costs cuts[i - 1], already known because it is a shorter prefix.
Store the prefix answer
Set cuts[j] = best and move to the next j. After the last letter, cuts[n - 1] is the answer for the whole string.
Code in Python, C++ and Java
Common pitfalls
Cutting off the longest palindrome first
Repeatedly remove the longest palindrome at the front of what is left.
For each prefix, try every palindromic last piece and keep the cheapest.
Grabbing the longest piece can force bad cuts later. For "abaab" the greedy split is aba | a | b with 2 cuts, but a | baab needs only 1.
Reading cuts[i - 1] when i is 0
best = min(best, cuts[i - 1] + 1)
best = 0 if i == 0 else min(best, cuts[i - 1] + 1)
When the whole prefix is a palindrome there is nothing before it and no cut is needed. In Python cuts[-1] silently reads the last slot, so "aba" returns 1 instead of 0; in C++ or Java the index is out of range.
Edge cases
Only single letters are palindromes, so every prefix keeps its worst case and the answer is n − 1.
The prefix of length one is a palindrome with i = 0, so the answer is 0.
Complexity
Palindrome Partitioning II FAQ
Can the memory drop below O(n²)?
Yes. Skip the table and grow palindromes outward from every centre, both single letters and gaps between letters. Each palindrome s[l..r] found updates cuts[r] with cuts[l - 1] + 1, or 0 when l is 0. Time stays O(n²) and only the cut array is stored.
Why does plain recursion time out?
Trying every first piece and recursing on the rest without remembering results solves the same suffix again and again, which grows exponentially. Caching the answer for each start index fixes the repeats, which is the same idea as the cut array here, run from the other end.
What changes from Palindrome Partitioning (number 131)?
Palindrome Partitioning wants every valid split written out, and there can be 2ⁿ⁻¹ of them, so it is a backtracking problem capped at 16 letters. Here only a count is returned, and counts for short prefixes are reused by longer ones.