Minimum ASCII Delete Sum for Two Strings
Problem Statement
You get two strings, s1 and s2. Delete some letters from each string, so that the two strings end up equal.
Deleting a letter costs its ASCII code (for example, a costs 97). Return the smallest total cost of the letters you delete.
Two strings s1 and s2.
The lowest total ASCII value of the letters that must be deleted to make the two strings equal.
Delete s (115) from sea and t (116) from eat. Both become ea, and 115 + 116 = 231. No other choice is cheaper.
Delete d, e, e (100 + 101 + 101) from delete to get let, and delete one e (101) from leet. The total is 403. Making both strings equal to lee or eet costs more.
- • 1 ≤ s1.length, s2.length ≤ 1000
- • s1 and s2 consist of lowercase English letters.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Recurse on (i, j): keep a matching pair for free, or delete one of the last letters
dp[i][j] = lowest deletion cost for the first i and first j letters; each cell is filled once
The same table in two rows: a cell only reads the row above and the cell to its left
Let cost(i, j) be the lowest ASCII sum to delete so that the first i letters of s1 and the first j letters of s2 become equal. If the last letters match, we may keep both: cost(i − 1, j − 1). We can always delete the last letter of s1 (its code plus cost(i − 1, j)), or the last letter of s2 (its code plus cost(i, j − 1)). Take the cheapest option. When one prefix is empty, delete all of the other one.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like DP — Strings translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.