22. Minimum Deletions
The problem can be found at the following link: ๐ Question Link
๐งฉ Problem Description
Given a string s
, the task is to determine the minimum number of characters that must be deleted from s
to convert it into a palindrome. The relative order of remaining characters must be preserved.
To solve this, we must maximize the length of the longest palindromic subsequence (LPS) and subtract its length from the original string length.
๐ฌ Key Insight:
The minimum number of deletions =
len(s) - LPS(s)
Where LPS(s) is the length of the Longest Palindromic Subsequence of
s
.
๐ Examples
Example 1:
Input:
s = "aebcbda"
Output:
2
Explanation:
The LPS is "abcba"
(length = 5). Minimum deletions = 7 - 5 = 2
.
Example 2:
Input:
s = "geeksforgeeks"
Output:
8
Explanation:
The LPS is "eeggee"
or "eefeef"
(length = 5). Deletions = 13 - 5 = 8
.
๐ Constraints
$1 \leq \text{length of } s \leq 10^3$
โ
My Approach
Bottom-Up 1D Dynamic Programming (Longest Palindromic Subsequence)
We compute the Longest Palindromic Subsequence (LPS) using dynamic programming. This optimized implementation uses only two 1D arrays (dp
and prev
) to store intermediate LPS lengths.
Algorithm Steps:
Initialize two 1D arrays of size
n
:dp
andprev
.Start from the end of the string and move backward:
For each character
s[i]
:Set
dp[i] = 1
(each character is a palindrome of length 1).Iterate
j
fromi+1
ton-1
:If
s[i] == s[j]
, setdp[j] = prev[j-1] + 2
.Else, set
dp[j] = max(prev[j], dp[j-1])
.
Copy
dp
toprev
for the next iteration.
Return
n - dp[n-1]
, which is the minimum number of deletions.
๐งฎ Time and Auxiliary Space Complexity
Expected Time Complexity: O(nยฒ), because we compute values for all pairs
(i, j)
wherei < j
.Expected Auxiliary Space Complexity: O(n), as we only use two arrays of size
n
.
๐ง Code (C++)
class Solution {
public:
int minDeletions(string &s) {
int n = s.size();
vector<int> dp(n), prev(n);
for (int i = n - 1; i >= 0; --i) {
dp[i] = 1;
for (int j = i + 1; j < n; ++j)
dp[j] = s[i] == s[j] ? prev[j - 1] + 2 : max(prev[j], dp[j - 1]);
prev = dp;
}
return n - dp[n - 1];
}
};
๐งโ๐ป Code (Java)
class Solution {
static int minDeletions(String s) {
int n = s.length();
int[] dp = new int[n], prev = new int[n];
for (int i = n - 1; i >= 0; --i) {
dp[i] = 1;
for (int j = i + 1; j < n; ++j)
dp[j] = s.charAt(i) == s.charAt(j) ? prev[j - 1] + 2 : Math.max(prev[j], dp[j - 1]);
prev = dp.clone();
}
return n - dp[n - 1];
}
}
๐ Code (Python)
class Solution:
def minDeletions(self, s):
n = len(s)
dp = [0] * n
prev = [0] * n
for i in range(n - 1, -1, -1):
dp[i] = 1
for j in range(i + 1, n):
dp[j] = prev[j - 1] + 2 if s[i] == s[j] else max(prev[j], dp[j - 1])
prev = dp[:]
return n - dp[-1]
๐ง Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: ๐ฌ Any Questions?. Letโs make this learning journey more collaborative!
โญ If you find this helpful, please give this repository a star! โญ
๐Visitor Count
Last updated