Day 39: Palindrome Partitioning
Palindrome Partitioning#
Welcome to Day 39 of our 60 Days of Coding Algorithm Challenge! Today, we’ll explore the Palindrome Partitioning problem, which combines string manipulation with dynamic programming concepts.
What is Palindrome Partitioning?#
Palindrome Partitioning is the problem of dividing a string into substrings, where each substring is a palindrome. A palindrome is a string that reads the same backward as forward.
Problem Statement#
Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.
Naive Recursive Approach#
Let’s start with a naive recursive implementation:
1def is_palindrome(s, start, end):
2 while start < end:
3 if s[start] != s[end]:
4 return False
5 start += 1
6 end -= 1
7 return True
8
9def palindrome_partition_recursive(s):
10 def backtrack(start, path):
11 if start == len(s):
12 result.append(path[:])
13 return
14
15 for end in range(start, len(s)):
16 if is_palindrome(s, start, end):
17 path.append(s[start:end+1])
18 …Keep your momentum going
Create a free account to unlock the full lesson, all 60 days, and track your progress as you go.
- Full access to all 60 daily lessons
- Track completion and build a daily streak
- Get interview-ready, one algorithm at a time
Join 2,900+ learners · 4.8/5 average rating