Problem Statement

Palindrome Partitioning II

You are given a string s. A palindrome is a word that reads the same forwards and backwards, like "aa" or "racecar". Your job is to chop the string into pieces so that every piece is a palindrome, and to do it with as few cuts as possible. A cut is just a slice between two letters. For example, "aab" can be split into ["aa", "b"], which takes 1 cut, and that is the fewest possible. The trick that makes this fast is to solve it in two stages. First we figure out, for every possible piece of the string, whether that piece is a palindrome. Then we use that knowledge to find the cheapest way to cut the whole string. Both stages use a technique called dynamic programming, which means we solve small pieces first, write down the answers, and reuse those answers to solve bigger pieces instead of recomputing them.

hardDynamic ProgrammingStringDynamic ProgrammingTime: O(n^2) · Space: O(n^2)

Signals to notice

minimum cuts to partition string into palindromesall substrings checked for palindrometwo DP layers

Brute force first

Try all possible partitions recursively. Each cut position is either used or not. That direct path helps you understand the question, but it tends to treat every possibility as brand new instead of learning from earlier steps.

The key insight

Two DPs: (1) isPalin[i][j] = is s[i.j] a palindrome? (2) dp[i] = min cuts for s[0.i]. For each i, if isPalin[0][i], dp[i] = 0. Otherwise dp[i] = min(dp[j] + 1) for all j where isPalin[j+1][i]. Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.

Trace it on s="aab"

Build isPalin (n=3): isPalin[0][1]=true("aa"), isPalin[1][2]=false("ab"), isPalin[0][2]=false (s[0]!=s[2])
Init dp = [0,1,2]  (dp[i]=i, worst case i cuts)
i=0: isPalin[0][0]=true -> dp[0]=0
i=1: isPalin[0][1]=true ("aa" is palindrome) -> dp[1]=0
i=2: isPalin[0][2]=false, so scan j: j=1 isPalin[1][2]=false; j=2 isPalin[2][2]=true -> dp[2]=min(2, dp[1]+1)=min(2,1)=1
dp = [0,0,1]; return dp[n-1]=dp[2]=1

What must stay true

dp[i] asks: what's the minimum cuts for the first i+1 characters? If the tail s[j+1.i] is a palindrome, then dp[i] = dp[j] + 1 (one cut after position j). Take the minimum over all valid j. As long as that statement keeps holding, you can trust the steps built on top of it.

Shape of the loop

isPalin = precompute palindrome table for all s[i..j]   # 2D DP
dp[i] = i  for all i                                     # at most i cuts
for i in 0..n-1:
    if isPalin[0][i]: dp[i] = 0                           # whole prefix is a palindrome
    else: for j in 1..i:
        if isPalin[j][i]: dp[i] = min(dp[i], dp[j-1] + 1) # cut before palindromic tail
return dp[n-1]

Pseudocode only — the full worked solution lives in the Solution tab.

Easy way to go wrong

Not precomputing the palindrome table — checking if each substring is a palindrome inline makes the solution. Precomputing isPalin is essential for. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.

Dynamic Programming Pattern