MEDIUM NC#77 Backtracking / DP

131. Palindrome Partitioning

📖 Problem

Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitionings 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
  • Apply Backtracking reasoning pattern

💡 Approach

  • Use backtracking to try all partitionings
  • Check if substring is palindrome before adding
  • Time: O(n * 2^n) worst case, Space: O(n) for result

🛠️ Hints & Pitfalls

Hints

  • Use backtracking to try all partitionings
  • Check if substring is palindrome before adding
  • Time: O(n * 2^n) worst case, Space: O(n) for result

🧪 Test Cases

Test Case 1
Not run
Input:
partition('aab');
Expected:
[["a","a","b"], ["aa","b"]]
Test Case 2
Not run
Input:
partition('a');
Expected:
[["a"]]
Test Case 3
Not run
Input:
partition(s: string);
Expected:
Computed from hidden reference

📝 Code Editor

📚 Reference Solution

⌘K Search ⌘↩ Run ⌘S Submit