Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 132 Palindrome Partitioning II - Walkthrough

Minimum cuts trace

Find minimum cuts to partition s="aab"s = \text{"aab"} into palindromes. First, precompute isPalin[i][j]isPalin[i][j] = true if s[i..j]s[i..j] is a palindrome. dp[i]dp[i] = min cuts for s[0..i]s[0..i].

If s[0..i]s[0..i] is palindrome, dp[i]=0dp[i] = 0. Else, try all cuts. dp[0]=0dp[0] = 0 ("a"). dp[1]=0dp[1] = 0 ("aa" is palindrome). dp[2]=1dp[2] = 1 ("aab" needs cut: "aa" + "b"). Pre-computing the palindrome table avoids redundant checks during the DP. This is a common preprocessing pattern.