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²)

šŸ› ļø 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

šŸ“š Reference Solution

ā–¼
⌘K Search āŒ˜ā†© Run ⌘S Submit