Problem of the day
You are given a string 'S'. Your task is to 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 only line of input contains a string 'S'.
Output Format :
For each test case, print all the possible palindromic partitions of the given string in a separate line.
Each substring of a partition is written within quotes(““) and separated by comma(,) and space, and each partition of the given string is written inside square brackets[].
The output of each test case will be printed in a separate line.
Note:
All the substrings of a partition are sorted in lexicographical order in the output. You just need to return the partitions in any order.
You do not need to print or sort anything, it has already been taken care of. Just implement the function.
0 <= |S|<= 15
where |S| denotes the length of string 'S'.
Time Limit: 1 sec.
aaC
["C", "a", "a"]
["C", "aa"]
For the given string "aaC" there are two partitions in which all substring of partition is a palindrome.
BaaB
["B", "B", "a", "a"]
["B", "B", "aa"]
["BaaB"]
For the given string "BaaB", there are 3 partitions that can be made in which every substring is palindromic substrings.
Can you check whether a substring is a palindrome each time in constant time?
O(N * 2 ^ N), where ‘N’ is the length of the String.
In the worst case, filling the dp takes O(N * N) initially and when all substrings are palindromes then there will be 2 ^ N possible substrings. For each possible substring, it takes O(N) to generate the substring and to check if it is palindrome it takes constant time. Hence the overall complexity will be O(N * 2 ^ N).
O(N ^ 2), where ‘N’ is the length of the string.
In the worst case, we will create a dp array of N * N dimensions thus O(N ^ 2) and also extra space is required for the recursion stack.