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:dpandprev.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
jfromi+1ton-1:If
s[i] == s[j], setdp[j] = prev[j-1] + 2.Else, set
dp[j] = max(prev[j], dp[j-1]).
Copy
dptoprevfor 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++)
⚡ Alternative Approaches
📊 2️⃣ LCS with Reversed String (Bottom-Up 2D DP)
Algorithm Steps:
Reverse the input string
sto gett.Compute the Longest Common Subsequence (LCS) between
sandtusing a 2D table.The LCS of
sand its reverse is the Longest Palindromic Subsequence (LPS).Return
s.length() - LPS.
✅ Why This Approach?
Classic LCS technique reused to find LPS.
Easy to understand and debug.
📝 Complexity Analysis:
Time: O(n²)
Auxiliary Space: O(n²)
📊 3️⃣ LCS with Reversed String (Space Optimized)
Algorithm Steps:
Reverse the string
sto gett.Use two 1D vectors to compute LCS between
sandtin a space-optimized way.Result =
s.length() - LCS.
✅ Why This Approach?
Minimizes memory use with only two rows.
Retains full accuracy and speed of 2D DP.
📝 Complexity Analysis:
Time: O(n²)
Auxiliary Space: O(n)
🆚 Comparison of Approaches
Approach
⏱️ Time
🗂️ Space
✅ Pros
⚠️ Cons
Original (1D DP from end)
🟢 O(n²)
🟢 O(n)
Compact, direct LPS logic
Harder to grasp for beginners
LCS with Reversed (2D DP)
🟢 O(n²)
🔸 O(n²)
Classic, easy to write
Higher memory usage
LCS with Reversed (Space Optimized)
🟢 O(n²)
🟢 O(n)
Balanced: space-efficient + clear
Slightly longer to implement
✅ Best Choice?
Scenario
Recommended Approach
Space optimized solution
🥇 Original 1D DP from end
Beginner-friendly or educational purpose
🥈 LCS with Reversed (2D DP)
Balanced readability + performance
🥉 LCS with Reversed (Optimized)
🧑💻 Code (Java)
🐍 Code (Python)
🧠 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