Palindrome Partitioning II
Minimum cuts so every piece of string s is a palindrome.
Intuition
Two DP layers. First, precompute pal[i][j] — is s[i..j] a palindrome? — by expanding shorter spans into longer ones (ends match + inside is a palindrome). Then cuts[i] = fewest cuts for the prefix ending at i: for every j where s[j..i] is a palindrome, the prefix before j plus one cut is a candidate; a palindromic full prefix needs zero.
Approach
Palindrome table first
pal[i][j] = (s[i]==s[j]) and (j−i < 2 or pal[i+1][j−1]). Filling by increasing length makes the inner lookup already available. O(n²) time and space.
Cuts DP on top
cuts[i] = 0 if s[0..i] is a palindrome, else min over all palindromic suffixes s[j..i] of cuts[j−1] + 1. Enumerating only palindromic last pieces is what the table enables in O(1) per check.
Why not backtracking
Enumerating all partitions (problem 131) is exponential; here only the count of cuts matters, so per-prefix minima collapse the search to O(n²).
Solution & live demo
Edge cases
cuts[n−1] = 0 via the full-prefix check.
Only single chars are palindromes — n−1 cuts.
0 cuts.