Palindrome Partitioning
Backtracking
Asked at 10 companies
7 recent
- Bloomberg
- Amazon
- Infosys
- Meta
- Microsoft
- Salesforce
- Coupang
- Visa
- DE Shaw
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 130 pointsSolved (Medium) 0/40Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Builds on: Building a new string or array as the answer · Replacing recursion with your own stack · Recursion and the call stack · The base case: where the calls stop · Depth-first traversal: preorder, inorder, postorder · Binary numbers and AND, OR, XOR
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · Sudoku solvers and regex: Try a choice, go deeper, and undo it when it fails. Solvers fill puzzles this way, and many regex engines match patterns the same way.
About this problem
Try a choice, recurse, undo it — subsets, permutations, and search.
Try it in the editor below for 20–30 minutes: passing its tests counts as solved and starts your reviews. The full statement is on LeetCode.
Examples
Full statement on LeetCode ↗Example 1
- Input
- s = "aab"
- Output
- [["a", "a", "b"], ["aa", "b"]]
Walk through it step by step(10 steps · shows the approach, so try first)
- Cut the string into pieces where every piece is a . Try each possible first piece; if it's a palindrome, keep it and cut the rest the same way.
- "a" reads the same both ways — take it as the next piece.
- "a" reads the same both ways — take it as the next piece.
- "b" reads the same both ways — take it as the next piece.
- Reached the end — ["a", "a", "b"] is a valid split.
- "ab" isn't a palindrome — can't cut here.
- "aa" reads the same both ways — take it as the next piece.
- "b" reads the same both ways — take it as the next piece.
- Reached the end — ["aa", "b"] is a valid split.
- "aab" isn't a palindrome — can't cut here.
Example 2
Single character- Input
- s = "a"
- Output
- [["a"]]
Example 3
- Input
- s = "efe"
- Output
- [["e", "f", "e"], ["efe"]]
- The answer can be in any order.
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: optimal solution
Solution code
Optimal solutionover cut positions: try every prefix that is a , then partition the rest. A precomputed table isPal[i][j] makes each palindrome check .
class Solution:
def partition(self, s: str) -> List[List[str]]:
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])
result, path = [], []
def build(start: int) -> None:
if start == n:
result.append(path[:])
return
for end in range(start, n):
if is_pal[start][end]:
path.append(s[start:end + 1])
build(end + 1)
path.pop()
build(0)
return resultReference solution, tested in Python, Java and C++.
Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.