


You are given a string S, partition S such that every substring of the partition is a palindrome. You need to return all possible palindrome partitioning of S.
Note: A substring is a contiguous segment of a string.
For Example :For a given string “BaaB.”
3 possible palindrome partitioning of the given string are:
{“B”, “a”, “a”, “B”}
{“B”, “aa”, “B”}
{“BaaB”}
Every substring of all the above partitions of “BaaB” is a palindrome.
The first line contains a single integer ‘T’ denoting the number of test cases. The test cases follow.
The first line of each test case contains a string S.
Output Format :
Print each palindromic partition of the given string in a separate line. Print the substrings of a string separated by a single space.
Note:
You do not need to print. It has already been taken care of. Just implement the function.
1 <= T <= 5
0 <= |S|<= 12
Where ‘T’ denotes the number of test cases, and |S| denotes the length of string S.
Time Limit: 1 sec
2
aaC
bb
a a C
aa C
b b
bb
In the first test case, there are two partitions in which all substrings of the partition are palindromes. The partitions are {“a”, ”a”, ”C”} and {“aa”, “C”}.
In the second test case, there are two partitions in which all substrings of the partition are palindromes. The partitions are {“b”, ”b”} and {“bb”}.
2
BaaB
abc
B a a B
B aa B
BaaB
a b c
In the first test case, there are three partitions in which all substrings of the partition are palindromes. The partitions are {“B”, ”a”, “a”, ”B”}, {“B”, “aa”, “B”}, {“BaaB”}.
In the second test case, there is only one partition in which all substrings of the partition are palindromes. The partition is {“a”, “b”, “c”}.
Can you try to generate all the partitions using recursion?
O((N^2)*(2^N)), where N is the length of the String.
In the worst case, when all substrings are palindromes. Then there will be 2^(N) possible substring partitions, and for each substring, it takes O(N) time to generate the substring and to check if it is palindrome or not it will again take O(N) time. Thus, the overall complexity will be O((N^2)*(2^N)).
O((N)*(2^N)), where N is the length of the string.
In the worst case, when all substrings are palindromes. Then there will be 2N possible partitions, and for each partition, we will need O(N) space to store the substrings. Thus, the total space complexity is O((N)*(2^N)).