You are given a string 'str' of length 'n'.
Find the minimum number of partitions in the string so that no partition is empty and every partitioned substring is a palindrome.
Input: 'str' = "aaccb"
Output: 2
Explanation: We can make a valid partition like aa | cc | b.
The first line contains the string 'str', the string to be partitioned.
Print the minimum number of cuts to be done so that each partitioned substring is a palindrome.
You do not need to print anything; it has already been taken care of. Just implement the given function.
aaccb
2
We can make a valid partition like aa | cc | b.
ababa
0
The string is already a palindrome, so we need not make any partition.
aababa
1
You can submit a solution of time complexity O(n ^ 3) but also try to write the solution having complexity O(n ^ 2).
1 <= 'n' <= 100
Time limit: 1 second
Find all possible ways to partition the string.
O(n * 2 ^ n), ‘n’ is the length of the string.
In the worst case, all possible combinations for length ‘n’ are recursively found, and in each case, we check if it is a palindrome that takes order of ‘n’ time.
O(n), ‘n’ is the length of the string.
Recursive stack uses space of the order ‘n’.