← Home

712. Minimum ASCII Delete Sum for Two Strings

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
dynamic programmingC++Markdown
712

Let's make this one less mysterious. For 712. Minimum ASCII Delete Sum for Two Strings, the solution in this repository is mainly a dynamic programming solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: dynamic programming.

The notes already sitting in the source point us in the right direction:

  • Approach #1: Dynamic Programming
  • time: O(M*N), space: O(M*N)

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are minimumDeleteSum.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • Substring checks are convenient but not free, so they are part of the real complexity story.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(M*N), space: O(M*N)
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Approach #1: Dynamic Programming 
02//Runtime: 48 ms, faster than 44.24% of C++ online submissions for Minimum ASCII Delete Sum for Two Strings.
03//Memory Usage: 15.4 MB, less than 77.78% of C++ online submissions for Minimum ASCII Delete Sum for Two Strings.
04//time: O(M*N), space: O(M*N)
05class Solution {
06public:
07    int minimumDeleteSum(string s1, string s2) {
08        int m = s1.size(), n = s2.size();
09        
10        vector<vector<int>> dp(m+1, vector(n+1, 0));
11        
12        //s1[i:] with s2[n:](empty string)
13        for(int i = m-1; i >= 0; i--){
14            dp[i][n] = dp[i+1][n] + (int)s1[i];
15        }
16        
17        //s1[m:](empty string) with s2[j:]
18        for(int j = n-1; j >= 0; j--){
19            dp[m][j] = dp[m][j+1] + (int)s2[j];
20        }
21        
22        for(int i = m-1; i >= 0; i--){
23            for(int j = n-1; j >= 0; j--){
24                // cout << s1.substr(i) << " " << s2.substr(j) << " ";
25                if(s1[i] == s2[j]){
26                    dp[i][j] = dp[i+1][j+1];
27                    // cout << "not delete" << endl;
28                }else{
29                    //whether delete from s1 or s2
30                    dp[i][j] = min(dp[i+1][j] + (int)s1[i], 
31                                  dp[i][j+1] + (int)s2[j]);
32                    // if(dp[i+1][j] + (int)s1[i] < dp[i][j+1] + (int)s2[j]){
33                    //     cout << "delete " << s1[i] << endl;
34                    // }else{
35                    //     cout << "delete " << s2[j] << endl;
36                    // }
37                }
38            }
39        }
40        
41        return dp[0][0];
42    }
43};
44
45//DP
46//Runtime: 44 ms, faster than 67.45% of C++ online submissions for Minimum ASCII Delete Sum for Two Strings.
47//Memory Usage: 15.1 MB, less than 77.78% of C++ online submissions for Minimum ASCII Delete Sum for Two Strings.
48class Solution {
49public:
50    int minimumDeleteSum(string s1, string s2) {
51        int m = s1.size();
52        int n = s2.size();
53        
54        vector<vector<int>> dp(m+1, vector(n+1, 0));
55        
56        for(int i = 0; i <= m; i++){
57            for(int j = 0; j <= n; j++){
58                if(i == 0 && j == 0){
59                    dp[i][j] = 0;
60                }else if(j == 0){
61                    dp[i][j] = dp[i-1][j] + s1[i-1];
62                }else if(i == 0){
63                    dp[i][j] = dp[i][j-1] + s2[j-1];
64                }else if(s1[i-1] == s2[j-1]){
65                    dp[i][j] = dp[i-1][j-1];
66                }else{
67                    dp[i][j] = min(
68                        dp[i-1][j] + s1[i-1],
69                        dp[i][j-1] + s2[j-1]
70                    );
71                }
72            }
73        }
74        
75        return dp[m][n];
76    }
77};

Cost

Complexity

Time
O(M*N), space: O(M*N)
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.