Skip to content
AI360Xpert
Beta
Difficulty: MediumBacktracking

Palindrome Partitioning

Problem in Plain English

Given a string s, return every way to cut it so each piece is a palindrome that reads the same forwards and backwards. Single characters always qualify, so an answer always exists.

Problem Statement

Given a string s, cut it into pieces so that every piece reads the same forwards and backwards.

Return all possible ways to cut the string that achieve this, where each way is a list of its palindromic pieces.

A single character always counts as a palindrome, so at least one partitioning always exists.

Constraints

  • 1 <= s.length <= 16
  • s consists of lowercase English letters.

Examples

Example 1

Input
s = "aab"
Output
[["a","a","b"],["aa","b"]]

The valid cuts give ["a","a","b"] and ["aa","b"]. Keeping "ab" or "aab" as a piece fails because their ends differ. These exhaust the four cut patterns.

Example 2

Input
s = "a"
Output
[["a"]]

The lone character is a palindrome by itself.

Example 3

Input
s = "aba"
Output
[["a","b","a"],["aba"]]

Both all-single-character pieces and the uncut string are valid. The cuts giving ["a","ba"] and ["ab","a"] fail. A longer prefix can be palindromic even when a shorter prefix is not.

Intuition

Each answer chooses cuts between characters, with no gaps or overlaps. For "aab", choosing "a" leaves "ab", which must split again; choosing "aa" leaves "b". Backtracking means append one valid piece, explore the remaining suffix, then remove the piece before trying another end. A palindrome table can answer whether a proposed piece is valid before the search enters that branch.

Approaches

Cut Enumeration

Solution Details

Reveal Cut Enumeration: intuition, complexity, and code

Hints

Hint 1
Think of every gap as a cut-or-keep decision. Pieces must cover the whole string in order.
Hint 2
At a suffix start, try each possible end and recurse only when that whole piece is palindromic. A failed shorter piece does not justify abandoning longer ends.
Hint 3
Precompute pal[i][j] from matching ends and the inner substring, then append, recurse from j + 1, and pop.

Edge Cases

  • A single character returns exactly one one-piece partition; there are no gaps to mask.
  • All equal characters make every one of the 2^(n-1) cut patterns valid; at n = 16 this is 32768 answers.
  • A string such as "abc" with no longer palindromes has only the all-single-character answer.
  • Odd palindromes such as "aba" and even palindromes such as "aa" exercise different interior base cases.

Common Mistakes and Interview Tips

  • Breaking the end loop after a non-palindromic prefix. In "aba", "ab" fails but the longer "aba" succeeds; skip that end and continue.
  • Reading pal[i + 1][j - 1] before computing it, or failing to short-circuit the length-one/two cases.
  • Saving the live path instead of a copy, so later pops corrupt already returned partitions.
  • Claiming O(n^2 times 2^n) for disjoint-piece scans: all pieces of one mask contain only n characters in total. Include output storage explicitly.

Key Takeaway

Partition searches pair well with precomputation: answer every repeated substring question once in a table, then let the walk spend its budget only on viable prefixes.