Palindrome Partitioning II - Problem

Given a string s, partition s such that every substring of the partition is a palindrome.

Return the minimum number of cuts needed for a palindrome partitioning of s.

A palindrome is a string that reads the same backward as forward.

Input & Output

Example 1 — Basic Partition
$ Input: s = "aab"
Output: 1
💡 Note: The string can be partitioned as "aa|b". "aa" and "b" are both palindromes, requiring only 1 cut.
Example 2 — Already Palindrome
$ Input: s = "aba"
Output: 0
💡 Note: The entire string "aba" is already a palindrome, so no cuts are needed.
Example 3 — Multiple Cuts Needed
$ Input: s = "abcde"
Output: 4
💡 Note: Each character must be its own partition: "a|b|c|d|e", requiring 4 cuts.

Constraints

  • 1 ≤ s.length ≤ 2000
  • s consists of lowercase English letters only

Visualization

Tap to expand
Palindrome Partitioning II: Find Minimum CutsInput:aab"aab"Cut here"aa""b"Both palindromes ✓Optimal partition: "aa" | "b"Minimum cuts needed: 1Dynamic programming finds the optimal solution efficiently
Understanding the Visualization
1
Input String
Given string that needs to be partitioned
2
Find Cuts
Determine optimal positions to make cuts
3
Verify Palindromes
Ensure each partition is a palindrome
4
Count Cuts
Return minimum number of cuts needed
Key Takeaway
🎯 Key Insight: Use DP to build minimum cuts incrementally, either with precomputed palindromes or expand-around-centers
Asked in
Google 45 Amazon 38 Microsoft 32 Facebook 28
125.0K Views
Medium-High Frequency
~25 min Avg. Time
3.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen