Generate All Palindromic Decompositions Of A String (Palindrome Partitioning on Leetcode) @BackToBackSWE
Generate All Palindromic Decompositions Of A String (Palindrome Partitioning on Leetcode)  @BackToBackSWE
Uploaded January 2019 | Updated November 2026, 3 days ago
Free 5-Day Mini-Course: backtobackswe.com
Try Our Full Platform: backtobackswe.com/pricing
๐Ÿ“น Intuitive Video Explanations
๐Ÿƒ Run Code As You Learn
๐Ÿ’พ Save Progress
โ“New Unseen Questions
๐Ÿ”Ž Get All Solutions

Question: Given a string, return all decompositions of the string that consist of only palindromes.

Example:

Input: "aab"
Output: [ ["aa","b"], ["a","a","b"] ]


Why is this problem backtracking?

It is backtracking because we will be expressing all of the possibilities of a search space given constraints. When we need to represent many decisions and possibilities, backtracking is the tool you should pull from your pocket.


Approach 1

Generate all decompositions and then only add the palindromic decompositions to our answer.

This is highly exponential in time since there are 2^(n - 1) palindromic decompositions.

This wastes a lot of time since we will continue to build on a decomposition that immediately disqualifies as palindromic.


Approach 2

We will take "snapshots" of snippets as we advance through the string and see if they can add to the decomposition that we want to build.

The 3 Keys To Backtracking:

Our Choice

The start and the end of a "snapshot" that we want to add to a decomposition we are working on.

Our Constraints

Each piece of the decomposition must be a palindrome, we cannot choose and advance on a non-palindrome snippet.

Our Goal

Decompose the whole string. When our decomposition progress index is the length of the array then we know that we have achieved this.


Complexities

Time: O(n * (2^n))

Worst case, but much better than approach 1 since this is a rare worst case where all decompositions turn out to be palindromic (a string of all 1 character).

Our best case becomes greatly improved.

We are basically taking subsets so (2^n) and the O(n) time to copy array to our answer.

Space: O(n)

At worst we will always go n stack frames deep in our recursion since an all single character decomposition is always a palindromic decomposition. This does not include the output array in space.

But let us imagine that we do include the output space.

We will have (2 ^ (n-1)) decompositions of the string.

We will certainly not have (2^n) PALINDROMIC decompositions for the string in a normal case.

The length of each decomposition will very likely not be n in the normal case (n/2 is probably a better approximation but I'm honestly not sure).

So approximate length for each decomposition: (n/2).
MAXIMUM decompositions we could ever make: (2 ^ (n-1)).

So this yields a VERY loose upper bound on the space with the output array included:

O( (n/2) * (2 ^ (n-1)) ) = O( n * 2^n)

REMEMBER THAT n IS THE # OF CHARACTERS IN THE STRING PASSED IN TO US.

I just want to get you thinking about this, it is all about demonstrating that you can think along the right lines and get back on track whether you are exactly right or not.

++++++++++++++++++++++++++++++++++++++++++++++++++

HackerRank: youtube.com/channel/UCOf7UPMHBjAavgD0Qw5q5ww

Tuschar Roy: youtube.com/user/tusharroy2525

GeeksForGeeks: youtube.com/channel/UC0RhatS1pyxInC00YKjjBqQ

Jarvis Johnson: youtube.com/user/VSympathyV

Success In Tech: youtube.com/channel/UC-vYrOAmtrx9sBzJAf3x_xw

++++++++++++++++++++++++++++++++++++++++++++++++++

This question is number 16.7 in "Elements of Programming Interviews" by Adnan Aziz, Tsung-Hsien Lee, and Amit Prakash.
Generate All Palindromic Decompositions Of A String (Palindrome Partitioning on Leetcode)Explained | Priority Queue Functions #shorts #datastructures #codinginterview #dsaLongest Common Subsequence (2 Strings) - Dynamic Programming & Competing SubproblemsBinary Tree Bootcamp: Full, Complete, & Perfect Trees. Preorder, Inorder, & Postorder Traversal.Knuthโ€“Morrisโ€“Pratt (KMP) Pattern Matching Substring Search -  First Occurrence Of SubstringThe Balanced Parentheses Problem - Classic Stack Problem (Valid Parentheses on Leetcode)3 Test Cases You Should Always Keep in Mind #shorts #datastructuresTotal Unique Ways To Make Change - Dynamic Programming (Coin Change 2 on LeetCode)How I Got An Internship @ TwitterThe Dutch National Flag Problem (The Quicksort Band-Aid)Search A 2D Sorted Matrix - Fundamentals of Search Space ReductionHow To Permute A String - Generate All Permutations Of A String
Back To Back SWE |

Generate All Palindromic Decompositions Of A String ("Palindrome Partitioning" on Leetcode)

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER