VOOZH about

URL: https://www.geeksforgeeks.org/dsa/palindrome-partitioning-dp-17/

⇱ Palindrome Partitioning - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Palindrome Partitioning

Last Updated : 23 Jul, 2025

Given a string s, the task is to find the minimum number of cuts needed for palindrome partitioning of the given string. A partitioning of the string is a palindrome partitioning if every sub-string of the partition is a palindrome.

Examples: 

Input: s = "geek" 
Output:
Explanation: We need to make minimum 2 cuts, i.e., "g | ee | k".

Input: s= "aaaa" 
Output: 0 
Explanation: The string is already a palindrome.

Input: s = "ababbbabbababa" 
Output: 3
Explanation: We need to make minimum 3 cuts, i.e., "aba | bb | babbab | aba".

[Naive Approach] Using Recursion - O(n*2^n) Time and O(n) Space

In this approach, we will try to apply all possible partitions and at the end return the correct combination of partitions.

This approach is similar to that of Matrix Chain Multiplication problem.

In this approach, we recursively evaluate the following conditions:

  • Base Case: If the current string is a palindrome, then we simply return 0, no Partitioning is required.
  • Else, like the Matrix Chain Multiplication problem,
    • we try making cuts at all possible places,
    • recursively calculate the cost for each cut
    • return the minimum value.

Output
2

[Better Approach 1] Using Top-Down DP (Memoization) - O(n^3) Time and O(n^2) Space

The above recursive approach has overlapping subproblems, leading to redundant computations thereby resulting in exponential time complexity. This redundant computation can be solved by using Memoization.

To avoid redundant computations, we can memoize results of each subproblem and reuse them as needed. A 2D array can serve as a memoization table to store solutions for overlapping subproblems. The size of this memo table will be n*n, as there are n possible starting indices and n possible ending indices for any subarray.


Output
2

[Better Approach 2] Using Bottom-Up DP (Tabulation) - O(n^3) Time and O(n^2) Space

The approach is similar to the previous one; just instead of breaking down the problem recursively, we iteratively build up the solution by calculating in bottom-up manner.

Here, we can use two 2D array dp[][] and isPalin[][], for storing the computed result.

  • dp[i][j] stores the minimum cuts for palindrome partitioning of the substring s[i ... j]
  • isPalin[i][j] tells us whether substring s[i ... j] is a palindromic string or not.

We starts with smaller substrings and gradually builds up to the result for entire string.


Output
2

[Expected Approach] Using Optimized Bottom-Up DP (Tabulation) - O(n^2) Time and O(n^2) Space

Here we precompute a palindrome table isPalin[][], where isPalin[i][j] = true if s[i..j] is a palindrome. This helps efficiently check palindrome partitions. Then we fill dp[], where dp[i] stores the minimum cuts needed to partition s[0..i] into palindromic substrings. If s[0..i] is a palindrome, no cut is needed (dp[i] = 0). Otherwise, we check all valid partitions and update dp[i] accordingly.

To fill dp[i], we find the suffix starting from j and ending at index i, (1 <= j <= i <= n - 1), which are palindromes. Hence, we can make a cut here that requires 1 + min cut from rest substring [0, j – 1]. For all such palindromic suffixes starting at j and ending at i, keep minimizing in dp[i]. Similarly, we need to compute results for all such i. (1 <= i <= n – 1) and finally, dp[n - 1] will be the minimum number of cuts needed for palindrome partitioning of the given string.


Output
2
Comment