Palindrome Partitioning
LeetCode 131 · Medium · solution, intuition & step-by-step walkthrough
Backtracking over palindromic prefixes; a dp table can remember which substrings are palindromes.
This is how I solved Palindrome Partitioning (LeetCode 131), a medium problem from the Partitioning pattern. Below: the intuition, the approach step by step, my accepted Python code and the 2 versions I wrote on the way, animated test cases and a line-by-line debugger.
Intuition
Cut the string from left to right. At each position try every end where s[start..end] is a palindrome, take it as the next piece, and recurse on the rest. Arriving at the end of the string means the current list of pieces is a valid partition.
Approach
- 1
backtrack(start, parts): if start == len(s), save a copy of parts.
- 2
For each end ≥ start, check s[start..end] is a palindrome.
- 3
Append it, recurse from end + 1, then pop.
- 4
Second version marks dp[start][end] using the ends-match and inner-range rule instead of scanning.
How I solved it · from first idea to final
Check each piece with is_palindrome.
dp marks palindromic ranges.
Backtracking + two-pointer checkPythonO(n·2ⁿ) time
class Solution:
def partition(self, s: str) -> List[List[str]]:
res=[]
def is_palindrome(start,end):
while start<end:
if s[start]!=s[end]:
return False
start+=1
end-=1
return True
def backtrack(start, cur_partitions):
if start == len(s):
res.append(cur_partitions[:])
return
for end in range(start,len(s)):
if is_palindrome(start,end):
cur_partitions.append(s[start:end+1])
backtrack(end+1,cur_partitions)
cur_partitions.pop()
backtrack(0,[])
return resBacktracking + palindrome tablePythonO(n·2ⁿ) time · O(n²) space
class Solution:
def partition(self, s: str) -> List[List[str]]:
res=[]
n = len(s)
dp=[[False]*n for _ in range(n)]
def backtrack(start, cur_partitions):
if start == len(s):
res.append(cur_partitions[:])
return
for end in range(start,len(s)):
if s[start]==s[end] and ((end-start)<=2 or dp[start+1][end-1]==True):
dp[start][end]=True
cur_partitions.append(s[start:end+1])
backtrack(end+1,cur_partitions)
cur_partitions.pop()
backtrack(0,[])
return resPalindrome Partitioning — FAQ
What is the intuition behind Palindrome Partitioning?
Cut the string from left to right. At each position try every end where s[start..end] is a palindrome, take it as the next piece, and recurse on the rest. Arriving at the end of the string means the current list of pieces is a valid partition.
How do you solve Palindrome Partitioning (LeetCode 131)?
1. backtrack(start, parts): if start == len(s), save a copy of parts. 2. For each end ≥ start, check s[start..end] is a palindrome. 3. Append it, recurse from end + 1, then pop. 4. Second version marks dp[start][end] using the ends-match and inner-range rule instead of scanning.
What is the time and space complexity of Palindrome Partitioning?
Time: O(n · 2ⁿ). Space: O(n²) for the table. (Accepted approach: Backtracking + palindrome table.)
What are common mistakes in Palindrome Partitioning?
Append a copy (parts[:]) — the list keeps changing. The inner check dp[start+1][end-1] is only valid for ranges already marked.
Is there more than one way to solve Palindrome Partitioning?
Backtracking + two-pointer check (O(n·2ⁿ) time): Check each piece with is_palindrome. Backtracking + palindrome table (O(n·2ⁿ) time · O(n²) space): dp marks palindromic ranges.