24. Palindromic Partitioning
The problem can be found at the following link: Question Link
My Approach
I use recursion and memoization DP to find the minimum number of cuts required to partition a given string into palindromic substrings.
I maintain a 2D DP array,
dp
, wheredp[i][j]
represents the minimum number of cuts needed to make the substringstr[i...j]
a palindrome.To compute
dp[i][j]
, I iterate over all possible substrings within the range(i, j)
.If the substring is already a palindrome,
dp[i][j]
is set to 0.If not, I calculate
dp[i][j]
by considering all possible cuts betweeni
andj
and choose the minimum.
Time and Auxiliary Space Complexity
Time Complexity:
O(n*n)
, where n is the length of the input stringstr
.Auxiliary Space Complexity:
O(n*n)
, as we use a 2D DP array of size n x n.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the getlost01/gfg-potd repository.
Last updated