Log In Sign Up
Day 39

Day 39: Palindrome Partitioning

39/60 Days

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: …