Skip to main content

Minimum ASCII Delete Sum for Two Strings - Solution & Explanation

MediumStringDynamic Programming19 min readAsked at: Amazon, Meta, Google +1
Practice this problem

Problem Statement

Given two strings s1 and s2, return the lowest ASCII sum of deleted characters to make two strings equal.

 

Example 1:

Input: s1 = "sea", s2 = "eat"
Output: 231
Explanation: Deleting "s" from "sea" adds the ASCII value of "s" (115) to the sum.
Deleting "t" from "eat" adds 116 to the sum.
At the end, both strings are equal, and 115 + 116 = 231 is the minimum sum possible to achieve this.

Example 2:

Input: s1 = "delete", s2 = "leet"
Output: 403
Explanation: Deleting "dee" from "delete" to turn the string into "let",
adds 100[d] + 101[e] + 101[e] to the sum.
Deleting "e" from "leet" adds 101[e] to the sum.
At the end, both strings are equal to "let", and the answer is 100+101+101+101 = 403.
If instead we turned both strings into "lee" or "eet", we would get answers of 433 or 417, which are higher.

 

Constraints:

  • 1 <= s1.length, s2.length <= 1000
  • s1 and s2 consist of lowercase English letters.

Approach Overview

Problem Overview: You are given two strings s1 and s2. Delete characters from either string so both become equal while minimizing the total ASCII value of deleted characters. The goal is not the number of deletions but the minimum sum of ASCII values removed.

Approach 1: Recursion with Memoization (Time: O(m*n), Space: O(m*n))

This approach models the problem as a recursive comparison of suffixes from both strings. At position i in s1 and j in s2, you either keep the characters if they match or delete one of them. If s1[i] == s2[j], move both pointers forward with no cost. Otherwise, try deleting s1[i] or s2[j] and add their ASCII values to the total cost. Store results for each state (i, j) in a memo table to avoid recomputation. Without memoization the recursion becomes exponential; caching reduces it to O(m*n). This approach clearly exposes the decision process and is useful when reasoning about overlapping subproblems in dynamic programming.

Approach 2: Dynamic Programming (Bottom-Up) (Time: O(m*n), Space: O(m*n))

The iterative DP solution builds a table dp[i][j] representing the minimum ASCII delete sum required to make s1[i:] and s2[j:] equal. Initialize the last row and column by deleting all remaining characters from the other string, accumulating ASCII values. Then iterate backward through both strings. If characters match, copy the value from dp[i+1][j+1]. If they differ, compute the minimum between deleting from s1 (ASCII(s1[i]) + dp[i+1][j]) or deleting from s2 (ASCII(s2[j]) + dp[i][j+1]). The final answer is dp[0][0]. This formulation is similar to the weighted version of the longest common subsequence problem and relies heavily on string comparison and dynamic programming state transitions.

The DP interpretation can also be viewed as maximizing the ASCII value of the common subsequence shared by both strings. Instead of explicitly building the subsequence, the algorithm directly computes the minimum cost of deletions. The bottom-up version avoids recursion overhead and is usually the most predictable implementation in interviews and production code.

Recommended for interviews: The bottom-up dynamic programming approach is the expected solution. It demonstrates clear state definition (dp[i][j]), correct transitions, and optimal O(m*n) complexity. Starting with the recursive memoized version shows understanding of the subproblem structure, but converting it into an iterative DP table signals stronger problem-solving maturity.

Approach 1: Dynamic Programming Approach

This approach involves using a 2D table to compute the minimum ASCII delete sum to make the two strings equal. The table is filled using a bottom-up dynamic programming method, considering the cost of deleting characters from either string.

For each index pair (i, j), we decide whether to delete a character from either s1 or s2 or take the sum from previously computed states to minimize the ASCII sum deletion cost.

This solution defines a 2D DP table where dp[i][j] represents the minimum ASCII delete sum to make the substrings s1[i:] and s2[j:] equal. We pre-compute the additional cost of deleting each character from the end of the strings and use these results to fill the DP table iteratively. If characters at positions i and j are equal, we take the diagonal value; otherwise, we take the minimum cost of deleting either character.

Code

Python

Java

C++

Complexity

Time Complexity: O(m * n), where m and n are the lengths of s1 and s2. This is because we need to fill the entire DP table.
Space Complexity: O(m * n), due to storage in a 2D table.

Try this approach in the editor →

Approach 2: Recursion with Memoization

This approach uses a recursive function with memoization to solve the problem. The recursive function computes the minimum ASCII sum by exploring all possible deletions and storing intermediate results to avoid redundant calculations. This can be a more intuitive approach for those familiar with recursion at the expense of higher time complexity when not optimized with memoization.

In this solution, we use a memoization dictionary to save results of previous calculations to avoid recomputation. The recursive helper function attempts deletions from s1 or s2 whenever theres is no character match, storing and reusing the computed results to find the minimum ASCII delete sum.

Code

C#

JavaScript

C

Complexity

Time Complexity: O(m * n) due to memoization reducing the duplicate calculations.
Space Complexity: O(m * n), with space used for the recursion stack and memoization storage.

Try this approach in the editor →

Approach 3: Dynamic Programming

We define f[i][j] as the minimum sum of ASCII values of deleted characters required to make the first i characters of s_1 equal to the first j characters of s_2. The answer is f[m][n].

If s_1[i-1] = s_2[j-1], then f[i][j] = f[i-1][j-1]. Otherwise, we can delete either s_1[i-1] or s_2[j-1] to minimize f[i][j]. Therefore, the state transition equation is as follows:

$ f[i][j]= \begin{cases} f[i-1][j-1], & s_1[i-1] = s_2[j-1] \ min(f[i-1][j] + s_1[i-1], f[i][j-1] + s_2[j-1]), & s_1[i-1] neq s_2[j-1] \end{cases}

The initial state is f[0][j] = f[0][j-1] + s_2[j-1], f[i][0] = f[i-1][0] + s_1[i-1].

Finally, return f[m][n].

The time complexity is O(m times n), and the space complexity is O(m times n). Where m and n are the lengths of s_1 and s_2$ respectively.

Similar problems:

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Dynamic Programming Approach

Time Complexity: O(m * n), where m and n are the lengths of s1 and s2. This is because we need to fill the entire DP table.
Space Complexity: O(m * n), due to storage in a 2D table.

Recursion with Memoization

Time Complexity: O(m * n) due to memoization reducing the duplicate calculations.
Space Complexity: O(m * n), with space used for the recursion stack and memoization storage.

Dynamic Programming—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Recursion with MemoizationO(m*n)O(m*n)When you want a clear top-down representation of subproblems or to quickly convert a recursive idea into an optimized solution.
Dynamic Programming (Bottom-Up)O(m*n)O(m*n)Preferred in interviews and production. Iterative table avoids recursion overhead and provides predictable performance.

Video Solution

Minimum ASCII Delete Sum for Two Strings | Intuition | Similar Problems | Leetcode 712 | MIK • codestorywithMIK • 12,960 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Minimum ASCII Delete Sum for Two Strings easy or hard?
The problem is classified as Medium on LeetCode. It requires recognizing that the task is a variation of the longest common subsequence problem but with ASCII weights. Once the DP state and transitions are defined, the implementation is straightforward.
Minimum ASCII Delete Sum for Two Strings Python/Java solution
Python, Java, and C++ implementations usually follow the same bottom-up DP logic. Create a 2D array dp[m+1][n+1], initialize base cases with ASCII sums, and fill the table from the end of both strings. The final result appears at dp[0][0].
How to solve Minimum ASCII Delete Sum for Two Strings in O(n)?
The standard solution requires O(m*n) time because every pair of indices from both strings must be considered. Space can be optimized to O(n) by keeping only two rows of the DP table at a time, but the time complexity remains O(m*n). Fully reducing the time to O(n) is not possible for the general case.
What is the best approach for Minimum ASCII Delete Sum for Two Strings?
Dynamic programming is the most reliable approach. Define a DP table where dp[i][j] represents the minimum ASCII delete cost to make the suffixes of two strings equal. By comparing characters and choosing the cheaper deletion, the algorithm computes the optimal result in O(m*n) time and O(m*n) space.
Is Minimum ASCII Delete Sum for Two Strings asked at Google/Amazon/Meta?
This problem is a classic dynamic programming pattern related to longest common subsequence and edit distance. Variants of it frequently appear in interviews at companies like Amazon, Google, and Meta where candidates are expected to design a DP state and optimize overlapping subproblems.
What data structure is used in Minimum ASCII Delete Sum for Two Strings?
The core data structure is a 2D dynamic programming table that stores results for every pair of indices in the two strings. Memoization implementations use a hash map or 2D array cache, while iterative solutions use a DP matrix to store intermediate costs.
What is the time complexity of Minimum ASCII Delete Sum for Two Strings?
The optimal dynamic programming solution runs in O(m*n) time, where m and n are the lengths of the two strings. Each state (i, j) is computed once and depends on constant-time transitions from neighboring states. Space complexity is also O(m*n) for the DP table or memoization cache.

Ready to solve this problem?

Practice Minimum ASCII Delete Sum for Two Strings with our built-in code editor and test cases.

Practice on FleetCode