Minimum ASCII Delete Sum for Two Strings - Problem

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

When you delete a character from either string, you add its ASCII value to the total cost. Your goal is to find the minimum total cost needed to make both strings identical.

Note: The final equal strings can be any string that can be formed by deleting characters from both input strings.

Input & Output

Example 1 — Basic Case
$ Input: s1 = "sea", s2 = "eat"
Output: 231
💡 Note: Delete 's' from "sea" (ASCII 115) and 't' from "eat" (ASCII 116). Both become "ea". Total cost: 115 + 116 = 231.
Example 2 — One String Empty
$ Input: s1 = "delete", s2 = ""
Output: 627
💡 Note: Delete all characters from "delete": d(100) + e(101) + l(108) + e(101) + t(116) + e(101) = 627.
Example 3 — Identical Strings
$ Input: s1 = "abc", s2 = "abc"
Output: 0
💡 Note: Strings are already identical, no deletions needed. Cost = 0.

Constraints

  • 1 ≤ s1.length, s2.length ≤ 1000
  • s1 and s2 consist of lowercase English letters only

Visualization

Tap to expand
Minimum ASCII Delete Sum for Two Strings INPUT s1 = "sea" s 115 e 101 a 97 s2 = "eat" e 101 a 97 t 116 Goal: Make both strings equal with minimum delete cost Common: "ea" Delete 's' from s1, 't' from s2 ALGORITHM STEPS 1 Create DP Table dp[i][j] = min cost for s1[0..i], s2[0..j] 2 Initialize Base Cases Sum of ASCII values for deletions 3 Fill DP Table If match: dp[i-1][j-1], else min 4 Return dp[m][n] Final answer at bottom-right DP Table (partial): "" e a t "" 0 101 198 314 s 115 216 313 429 e 216 115 212 328 a 313 212 115 231 FINAL RESULT Minimum ASCII Delete Sum: 231 Deleted Characters: From s1: delete 's' ASCII('s') = 115 From s2: delete 't' ASCII('t') = 116 Total: 115 + 116 = 231 Both become: "ea" OK - Strings Match! Key Insight: This is a variant of Longest Common Subsequence (LCS). Instead of maximizing matches, we minimize deletion costs. dp[i][j] represents the minimum ASCII sum to make s1[0..i-1] and s2[0..j-1] equal. Time Complexity: O(m*n) | Space Complexity: O(m*n) where m, n are string lengths. TutorialsPoint - Minimum ASCII Delete Sum for Two Strings | Optimal DP Solution
Asked in
Google 15 Microsoft 12 Amazon 8 Facebook 6
89.5K Views
Medium Frequency
~25 min Avg. Time
2.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen