Coding interview problems › Backtracking
🧩 Backtracking Medium
Palindrome Partitioning
Palindrome Partitioning explained: backtrack over cut positions, keep only palindromic pieces and list every partition, with tested Python and pitfalls.
Also searched as: palindrome partitioning backtracking · split a string into palindromes · all palindromic partitions of a string
Try it live Free
Backtracking: Subsets, Permutations & Pruning
Turn pruning on in the Backtracking lab and watch whole subtrees disappear; here every non-palindromic prefix prunes its subtree the same way.
Also useful: 2D DP Table: LCS & Edit Distance
The problem
Given a string, return every way to cut it into pieces such that every piece is a palindrome.
The key insight
Choose where the first piece ends. If that prefix is a palindrome, keep it and partition the rest of the string the same way; if not, that branch is pruned immediately. A precomputed table of which substrings are palindromes makes each check O(1).
Step-by-step approach
- Optionally precompute
is_pal[i][j]for all substrings: true ifs[i] == s[j]and the inside is a palindrome. - Define
dfs(start): ifstart == len(s), record a copy ofpath. - For each end
efromstartto the last index, ifs[start..e]is a palindrome: append it, calldfs(e + 1), pop. - Call
dfs(0).
Python solution
def partition(s):
n = len(s)
is_pal = [[False] * n for _ in range(n)]
for i in range(n - 1, -1, -1):
for j in range(i, n):
is_pal[i][j] = s[i] == s[j] and (j - i < 2 or is_pal[i + 1][j - 1])
out, path = [], []
def dfs(start):
if start == n:
out.append(path[:])
return
for end in range(start, n):
if is_pal[start][end]: # prune non-palindromic pieces
path.append(s[start:end + 1])
dfs(end + 1)
path.pop()
dfs(0)
return outComplexity
A string of identical letters has 2n-1 partitions, each copied in O(n). The palindrome table is O(n²) space.
Common mistakes
- Re-checking each piece with slicing and reversal every time, which adds an O(n) factor per check.
- Filling
is_palin the wrong order: rowineeds rowi + 1, soimust go from the end backwards. - Returning the count when the problem asks for the partitions themselves (the minimum-cuts variant is a separate DP).
Frequently asked questions
What is the time and space complexity of Palindrome Partitioning?
O(n × 2^n) time in the worst case (all equal letters), plus O(n²) to build the palindrome table, which is also the main extra space.
How do I find the minimum number of cuts instead?
That is Palindrome Partitioning II, a DP: cuts[i] = min(cuts[j - 1] + 1) over every j where s[j..i] is a palindrome. It runs in O(n²).
Do I need the palindrome table?
No, checking each piece directly is still correct. The table removes repeated work and is a nice way to show interviewers you see the overlapping subproblems.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Backtracking Backtracking: Subsets, Permutations & Pruning lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.