![]() |
VOOZH | about |
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 stringInput: s = "aba"
Output: 0
Explanation: We don't remove any character.
Table of Content
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]
2
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], thenminDel(i, j, s, memo) = minDel(i+1, j-1, s, memo)(we don't need to delete either character)- If
s[i] != s[j], thenminDel(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:
i and j), we create a 2D memo array to store the results of previously solved subproblems.s[i...j] a palindrome.memo array with -1 to indicate that no computation has been done for that subproblem yet.2
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:
2
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:
- The value from two rows back at position dp[i+1][j-1] (when characters match)
- 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)
2
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.
2