VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-number-deletions-make-string-palindrome/

⇱ Minimum Deletions to Make a String Palindrome - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum Deletions to Make a String Palindrome

Last Updated : 5 May, 2025

Given a string s of length n, the task is to remove or delete the minimum number of characters from the string so that the resultant string is a palindrome. 

Note: The order of characters should be maintained. 

Examples :

Input : s = "aebcbda"
Output : 2
Explanation: Remove characters 'e' and 'd'. Resultant string will be "abcba" which is a palindromic string

Input: s = "aba"
Output: 0
Explanation: We don't remove any character.

[Naive Approach] Using Recursion - O(2^n) time and O(n) space

The idea is to compare the corner characters first. If the starting index i and ending index j of substring match, then recur for i + 1, j - 1. Otherwise, recur for (i + 1, j) and (i, j - 1) and return the minimum out of these two plus 1.

Recurrence relation will be:

  • If s[i] equals s[j], we don't need to delete either character, so we recursively check the substring s[i+1...j-1].
  • If s[i] is not equal to s[j], we have two choices: delete either s[i] or s[j], and take the minimum of these two options.

This can be expressed as:

  • minDel(i, j) = 0, if i >= j (base case)
  • minDel(i, j) = minDel(i+1, j-1), if s[i] == s[j]
  • minDel(i, j) = 1 + min(minDel(i+1, j), minDel(i, j-1)), if s[i] != s[j]

Output
2

[Better Approach - 1] Using Top-Down DP (Memoization) – O(n^2) time and O(n^2) space

1. Optimal Substructure: The minimum number of deletions required to make substring s[i...j] a palindrome, i.e., minDel(i, j, s, memo), depends on the optimal solutions of the subproblems. There are two cases:

  • If s[i] == s[j], then minDel(i, j, s, memo) = minDel(i+1, j-1, s, memo) (we don't need to delete either character)
  • If s[i] != s[j], then minDel(i, j, s, memo) = 1 + min(minDel(i+1, j, s, memo), minDel(i, j-1, s, memo)) (we delete either the first or last character, whichever gives the minimum result)

2. Overlapping Subproblems: While applying a recursive approach to this problem, we notice that certain substring calculations are computed multiple times, especially for longer strings.

Follow the below steps to implement the idea:

  • Since there are two parameters that change during recursive calls (i and j), we create a 2D memo array to store the results of previously solved subproblems.
  • memo[i][j] will represent the minimum number of deletions required to make the substring s[i...j] a palindrome.
  • We initialize the memo array with -1 to indicate that no computation has been done for that subproblem yet.

Output
2

[Better Approach - 2] Using Bottom-Up DP (Tabulation) – O(n^2) time and O(n^2) space

The idea is to fill the DP table based on previously computed values. For each position pair (i,j) in the string, we determine the minimum number of deletions needed to make the substring s[i...j] a palindrome. The table is filled in an iterative manner by first handling smaller substrings and gradually building up to the entire string.

The dynamic programming relation is as follows:

  • If s[i] == s[j], then dp[i][j] = dp[i+1][j-1] (no deletion needed for matching characters)
  • If s[i] != s[j], then dp[i][j] = 1 + min(dp[i+1][j], dp[i][j-1]) (delete either the first or last character, whichever gives the minimum result)

Output
2

[Efficient Approach - 1] Using Space Optimized DP - O(n^2) time and O(n) space

In the previous approach of dynamic programming, we derived the relation between states as given below:

  • If s[i] == s[j], then dp[i][j] = dp[i+1][j-1] (characters match, no deletion needed)
  • else dp[i][j] = 1 + min(dp[i+1][j], dp[i][j-1]) (characters don't match, delete one)

If we observe the recurrence relation carefully, for calculating the current dp[i][j] state, we need:

  1. The value from two rows back at position dp[i+1][j-1] (when characters match)
  2. Values from the previous row at position dp[i+1][j] and current row at position dp[i][j-1] (when characters don't match)

Due to this dependency pattern, we need to maintain three arrays instead of the full 2D matrix:

  • prev2: stores the values from two rows back (l-2)
  • prev1: stores the values from the previous row (l-1)
  • curr: stores the values for the current row (l)

Output
2

[Expected Approach - 2] Using Length of Longest Palindromic Subsequence - O(n^2) time and O(n) space

The idea is to find the length of the longest palindromic subsequence, and then subtracting this length from the total length of string will give the minimum number of deletions to make the string palindrome.


Output
2
Comment