Palindrome Partitioning
Palindrome Partitioning is LeetCode 131 (Medium). Cut the string s into pieces so that every piece is a palindrome, and return every way of doing it. In other words, list all the palindromic partitions of s, in any order.
- A single letter is always a palindrome, so there is always at least one answer: every letter on its own.
shas at most 16 letters. A string of one repeated letter has 2¹⁵ valid splits, so the output alone can be huge, and a search that lists them one by one is the expected approach.
- 1 <= s.length <= 16
- s contains only lowercase English letters.
Intuition
Build each answer from left to right. The pieces chosen so far cover a prefix of s, and the next piece must start exactly where they stop. Try every palindrome that starts there, recurse on what is left, then take the piece back and try the next one. When the pieces reach the end of the string, they form one complete answer.
- Pruning: a piece that is not a palindrome is skipped on the spot, so no branch ever holds a bad piece.
- Constant-time checks: the same substring is tested in many branches, so a table
pal[i][j]built once up front answers each test immediately.
"Return all ways to split" or "list every partition" with a small input size points to backtracking: choose a piece, recurse on the rest, undo the choice.
Approach
Before reading on, write down every split of "aab" by hand and notice where you first decide something. What is the next decision once the first piece is fixed? Aim for a search that never builds a split containing a non-palindrome.
Build the palindrome table
Set pal[i][j] to true when s[i] == s[j] and the part strictly between them is a palindrome (or there is nothing between them). Fill i from the end so that pal[i + 1][j - 1] is ready when it is read.
Choose the next piece
In dfs(start), loop end from start to the last index. When pal[start][end] is true, add s[start..end] to the current path. Only palindromes are ever added, so the path is always a valid partial answer.
Recurse on the rest of the string
Call dfs(end + 1). What remains is the same task on a shorter suffix, and the rules for it do not depend on how the prefix was cut.
Record a finished split
When start equals the length, the pieces cover the whole string. Append a copy of the path to the answers, because the path itself keeps changing as the search goes on.
Undo the choice
Remove the last piece after the recursive call returns. The loop then tries a longer piece from the same start, on top of the same prefix as before.
Code in Python, C++ and Java
Common pitfalls
Saving the path itself instead of a copy
res.append(path)
res.append(path[:])
Every saved entry is the same list object, and the undo steps empty it before the function returns. For s = "aab" the output becomes [[], []] instead of [["a", "a", "b"], ["aa", "b"]].
Filling the table from the front
for i in range(n):
for i in range(n - 1, -1, -1):
pal[i][j] reads pal[i + 1][j - 1], a row further down. Going from the front reads that row before it is filled. For s = "aba" the whole string looks like a non-palindrome and the answer ["aba"] goes missing.
Edge cases
The only split is the letter itself, so the result is [[s]].
Every longer piece fails the table check, so the search walks straight down one branch and returns the single split into letters.
Complexity
Palindrome Partitioning FAQ
Do I need the table, or can I check each piece directly?
A two-pointer check on each piece also passes, since n is at most 16. It costs up to O(n) per check, and the same piece is checked again in every branch that reaches it. The table pays O(n²) once so that every later check is a lookup.
How is this different from Palindrome Partitioning II?
Palindrome Partitioning II asks only for the smallest number of cuts, not the splits themselves. That count can be found with an O(n²) table, so it allows strings of 2,000 letters, far beyond what listing every split could handle.