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.
A palindrome string is a string that reads the same backward as forward.

Example 1:
Input: s = "aab"
Output: [["a","a","b"],["aa","b"]]

Example 2:
Input: s = "a"
Output: [["a"]]

Solution:




Having a strong grip on Backtracking Template is an absolute necessity to solve this problem, if you are new to Backtracking.

We need to cut the string at all possible places to form the first palindromic substring and for each of this palindromic substring we explore the rest of the given string to look for other palindromic substrings.

So here the candidates would be indices in the given string that would give us a palindromic substring starting from the start index, so
beg = start index
and end = candidate index

Let's look at the code below and the inline comments for better understanding of our algorithm. The code adheres to our Backtracking Template.

Java Solution:



Login to Access Content



Python Solution:



Login to Access Content




Don't forget to take in-depth look at the other backtracking problems because that is what would make you comfortable with using the backtracking template and master the art of Backtracking:


Instructor:



If you have any feedback, please use this form: https://thealgorists.com/Feedback.



Help Your Friends save 40% on our products

wave