![]() |
VOOZH | about |
Given a string, find all possible palindromic partitions of a given string.
Note that this problem is different from Palindrome Partitioning Problem, there the task was to find the partitioning with minimum cuts in input string. Here we need to print all possible partitions.
Example:
Input: nitin
Output: n i t i n
n iti n
nitinInput: geeks
Output: g e e k s
g ee k s
Please refer Print all Palindromic Partitions of a String using Backtracking for Backtracking approach for this problem.
The idea is to consider the space between two characters in the string.
- If there n characters in the string, then there are n-1 positions to put a space or say cut the string.
- Each of these positions can be given a binary number 1 (If a cut is made in this position) or 0 (If no cut is made in this position).
- This will give a total of 2n-1 partitions and for each partition check whether this partition is palindrome or not.
Illustration:
Input : geeks
0100=> ["ge","eks"] (not valid)1011=> ["g","ee","k","s"] (valid)1111=> ["g","e","e","k","s"] (valid)0000=> ["geeks"] (not valid)
Below is the implementation of the idea.
g e e k s g ee k s
Time complexity: O(n*2n), n is size of the string.
Auxiliary Space: O(2n), to store all possible partition of string.