HARD 2-D Dynamic Programming
132. Palindrome Partitioning II
š Problem
Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s.
š§ Visual Learning Aid
1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases
JS/TS Refreshers
- ā¢Array methods (`push`, `pop`, `shift`, `slice`)
- ā¢Object/Map/Set usage patterns
- ā¢Function parameter and return typing
- ā¢Array DP table updates
- ā¢State transition thinking
- ā¢Base case initialization
Logical Thinking Concepts
- ā¢Define invariants before coding
- ā¢Check edge cases first (`[]`, single element, duplicates)
- ā¢Estimate time/space before implementation
- ā¢Apply Dynamic Programming reasoning pattern
š” Approach
- ā Use dp[i][j] = min cuts for s[i:j+1] to make palindromes
- ā Base case: dp[i][i] = 0 (single char is palindrome)
- ā Check if s[i:j+1] is palindrome before computing dp[i][j]
- ā dp[i][j] = min(dp[i][k] + dp[k+1][j]) for all k where s[i:k+1] is palindrome
- ā Time: O(n³), Space: O(n²)
š§ Prerequisites
š ļø Hints & Pitfalls
Hints
- ā¢Use dp[i][j] = min cuts for s[i:j+1] to make palindromes
- ā¢Base case: dp[i][i] = 0 (single char is palindrome)
- ā¢Check if s[i:j+1] is palindrome before computing dp[i][j]
Common Pitfalls
- ā¢dp[i][j] = min(dp[i][k] + dp[k+1][j]) for all k where s[i:k+1] is palindrome
- ā¢Time: O(n³), Space: O(n²)
š§Ŗ Test Cases
Hidden tests on submit: 1
Test Case 1
Not run Input:
partition('aab'); Expected:
false Test Case 2
Not run Input:
partition('a'); Expected:
false Test Case 3
Not run Input:
partition('ab'); Expected:
false š Code Editor
š¤ Output