CMD Guide
HomeDSADynamic Programming

Minimum Deletions in a String to make it a Palindrome

Mechanism

A string of length n can always be turned into a palindrome by deleting characters; the fewest deletions equal n minus the length of the longest palindromic subsequence (LPS) already hiding inside it. Keeping the LPS untouched and deleting every other character is both sufficient and optimal, because any palindrome formed by deletion is itself a palindromic subsequence of the original string — so the largest one you can preserve is the LPS, and everything else must go.

Recognize the pattern

Brute force → optimal

Brute force: enumerate every subsequence, check if it is a palindrome, keep the longest one, then answer = n minus that length. There are 2ⁿ subsequences, each checked in O(n), giving O(2ⁿ·n) time — usable only for tiny n.

Optimal: reduce to the Longest Palindromic Subsequence (LPS) of s, computed either directly with interval DP or as the Longest Common Subsequence (LCS) of s and reverse(s) — a palindromic subsequence read forwards equals itself read backwards, so it is also a common subsequence of s and its reverse. Both formulations run in O(n²) time and O(n²) space (reducible to O(n) space).

Complexity, derived from first principles

Let dp[i][j] = length of the LPS inside substring s[i..j]. There are O(n²) index pairs (i ≤ j), and each cell is filled in O(1) from smaller subproblems:

dp[i][i] = 1
if s[i] == s[j]: dp[i][j] = dp[i+1][j-1] + 2
else:            dp[i][j] = max(dp[i+1][j], dp[i][j-1])

O(n²) cells × O(1) work = O(n²) time. The table itself needs O(n²) space; since row i only reads rows i and i+1, this can be compressed to O(n) with two rolling arrays. Final answer = n − dp[0][n-1].

Traced example: "cddpd" (n = 5)

Indices: c(0) d(1) d(2) p(3) d(4). Filling dp by increasing substring length:

Substring (i..j)s[i], s[j]dp[i][j]Why
len 1: all i..i-1single char is trivially a palindrome
(1,2) "dd"d, d2match → dp[2][1]+2, base case treated as 0+2
(2,3) "dp"d, p1mismatch → max(dp[3][3], dp[2][2]) = 1
(3,4) "pd"p, d1mismatch → max = 1
(1,4) "ddpd"d, d3match → dp[2][3]+2 = 1+2 = 3
(0,4) "cddpd"c, d3mismatch → max(dp[1][4], dp[0][3]) = max(3, 2) = 3

LPS length = dp[0][4] = 3 (the subsequence "ddd" from positions 1,2,4). Minimum deletions = 5 − 3 = 2, matching the worked answer of deleting "c" and "p".

Java implementation

class Solution {
    public int minDeletions(String s) {
        int n = s.length();
        int[][] dp = new int[n][n];
        for (int i = 0; i < n; i++) dp[i][i] = 1;
        for (int len = 2; len <= n; len++) {
            for (int i = 0; i + len - 1 < n; i++) {
                int j = i + len - 1;
                if (s.charAt(i) == s.charAt(j)) {
                    dp[i][j] = (len == 2 ? 0 : dp[i + 1][j - 1]) + 2;
                } else {
                    dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]);
                }
            }
        }
        int lps = n == 0 ? 0 : dp[0][n - 1];
        return n - lps;
    }
}

Pitfalls

When to use / when not — trade-offs

Use interval DP on dp[i][j] (or LCS with the reverse) whenever the problem asks for a count of edits (insertions and/or deletions, which are symmetric here) to reach a palindrome, for n up to a few thousand. It is simple, correct, and O(n²) time/space.

vs. Manacher's algorithm: Manacher finds the longest palindromic substring (contiguous) in O(n), but that is a different quantity — it cannot answer this question, since the kept characters here need not be contiguous.

vs. plain LCS(s, reverse(s)): mathematically equivalent and often already implemented as a library routine, but costs an extra O(n) reversal and an extra mental mapping step; the direct LPS recurrence avoids that indirection and is easier to trace during an interview.

Takeaways

Recall: Why does keeping the longest palindromic subsequence and deleting the rest always give the minimum number of deletions?


Compiled from standard interview-prep DP treatments of Longest Palindromic Subsequence and its reduction to LCS(s, reverse(s)).

🤖 Don't fully get this? Learn it with Claude

Stuck on Minimum Deletions in a String to make it a Palindrome? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Minimum Deletions in a String to make it a Palindrome** (DSA) and want to truly understand it. Explain Minimum Deletions in a String to make it a Palindrome from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Minimum Deletions in a String to make it a Palindrome** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Minimum Deletions in a String to make it a Palindrome** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Minimum Deletions in a String to make it a Palindrome** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes