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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code