Palindrome Partitioning - Problem

Given a string s, your task is to partition it into substrings such that every substring in the partition is a palindrome. A palindrome reads the same forwards and backwards, like "racecar" or "aba".

You need to return all possible palindrome partitionings of the string. Think of it as finding every way to "cut" the string into pieces where each piece is a palindrome.

Example: For string "aab":
โ€ข One partitioning: ["a", "a", "b"] - each single character is a palindrome
โ€ข Another partitioning: ["aa", "b"] - "aa" is a palindrome, "b" is a palindrome

The challenge is to find all such valid partitionings efficiently!

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "aab"
โ€บ Output: [["a","a","b"],["aa","b"]]
๐Ÿ’ก Note: There are two ways to partition "aab": split each character individually ["a","a","b"] where each is a palindrome, or group the first two characters ["aa","b"] since "aa" is a palindrome.
example_2.py โ€” Single Character
$ Input: s = "raceacar"
โ€บ Output: [["r","a","c","e","a","c","a","r"],["r","a","c","e","aca","r"],["r","a","cec","a","r"],["r","aca","c","a","r"],["raceacar"]]
๐Ÿ’ก Note: Multiple valid partitions exist: all single characters, grouping "aca", grouping "cec", or the entire string "raceacar" which is itself a palindrome.
example_3.py โ€” Edge Case
$ Input: s = "a"
โ€บ Output: [["a"]]
๐Ÿ’ก Note: A single character string has only one partition - the character itself, which is always a palindrome.

Visualization

Tap to expand
Palindrome Partitioning ProcessStep 1: Preprocessing - Build Palindrome DictionaryInput: "aab"Palindrome Check Results:"a" โœ“"a" โœ“"b" โœ“"aa" โœ“"ab" โœ—"aab" โœ—Step 2: Backtracking ExplorationPath 1: Split each character"a""a""b"โœ“ Valid partition: ["a", "a", "b"]Path 2: Group first two characters"aa""b"โœ“ Valid partition: ["aa", "b"]Path 3: Use entire string"aab"โœ— Not a palindrome - backtrack!Step 3: Final ResultsAll Valid Palindrome Partitions:["a", "a", "b"]["aa", "b"]๐Ÿ’ก Key Insight:Preprocessing eliminates redundant palindrome checks during backtracking,making the algorithm much more efficient for strings with repeated patterns.
Understanding the Visualization
1
Preprocessing Phase
Build a lookup table of all palindromic substrings, like creating a dictionary of valid words
2
Exploration Phase
Use backtracking to try different 'cuts' in the string, consulting our palindrome dictionary
3
Decision Making
At each position, decide whether to make a cut or extend the current substring
4
Backtrack and Continue
If we hit a dead end (non-palindrome), backtrack and try the next possibility
Key Takeaway
๐ŸŽฏ Key Insight: The combination of DP preprocessing with backtracking gives us the best of both worlds - we avoid redundant computation while systematically exploring all valid partitions. The preprocessing step is crucial for efficiency when the same substrings are checked multiple times.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(Nยฒ + 2^N)

O(Nยฒ) for building DP table to check all substrings, plus O(2^N) for generating all possible partitions

n
2n
โœ“ Linear Growth
Space Complexity
O(Nยฒ)

O(Nยฒ) for the DP table storing palindrome information, plus O(N) recursion stack

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค s.length โ‰ค 16
  • s contains only lowercase English letters
  • Note: The relatively small constraint on length makes backtracking feasible
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28
89.0K Views
High Frequency
~25 min Avg. Time
2.2K 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