mediumBacktrackingAmazonGoogle
Palindrome Partitioner
A linguistic analyzer segments a text into contiguous slices that each read the same forwards and backwards.
Given a string s, partition it such that every substring is a palindrome. Return all possible partitions.
Choose approach
a
0ij
a
1b
2SetPalindromic partitions (0)
empty
1/7
Narrationstep 1 of 7
Pick palindrome "a".
Variables
cur=[a]
out=0
How to think about it
- Palindrome check: two pointers from both ends.
- Only recurse on palindromic prefixes -> prune non-palindrome branches.
- Emit the current list when we consume the entire string.
Edge cases to catch in interview
- Single character -> itself is a palindrome.
- All identical chars -> every slice is a palindrome.
Examples
Example 1
[["a","a","b"],["aa","b"]]{"s":"aab","values":["a","a","b"]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed