The trick here is to name the state correctly, then let the implementation follow. For 583. Delete Operation for Two Strings, the solution in this repository is mainly a dynamic programming solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. 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:
- DP, O(N^2) space
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are minDistance, LCS.
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.
- 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:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
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(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//DP, O(N^2) space
02//Runtime: 28 ms, faster than 53.48% of C++ online submissions for Delete Operation for Two Strings.
03//Memory Usage: 12 MB, less than 55.56% of C++ online submissions for Delete Operation for Two Strings.
04class Solution {
05public:
06 int minDistance(string word1, string word2) {
07 int m = word1.size();
08 int n = word2.size();
09
10 vector<vector<int>> dp(m+1, vector(n+1, 0));
11
12 for(int i = 0; i <= m; i++){
13 for(int j = 0; j <= n; j++){
14 if(i == 0 && j == 0){
15 dp[i][j] = 0;
16 }else if(i == 0){
17 dp[i][j] = j;
18 }else if(j == 0){
19 dp[i][j] = i;
20 }else if(word1[i-1] == word2[j-1]){
21 dp[i][j] = dp[i-1][j-1];
22 }else{
23 dp[i][j] = min(
24 dp[i-1][j], //remove word1[i-1]
25 dp[i][j-1] //remove word2[j-1]
26 ) + 1;
27 }
28 }
29 }
30
31 return dp[m][n];
32 }
33};
34
35//DP, O(N) space
36//Runtime: 20 ms, faster than 73.66% of C++ online submissions for Delete Operation for Two Strings.
37//Memory Usage: 6.7 MB, less than 100.00% of C++ online submissions for Delete Operation for Two Strings.
38class Solution {
39public:
40 int minDistance(string word1, string word2) {
41 int m = word1.size();
42 int n = word2.size();
43
44 vector<int> dp(n+1, 0);
45
46 int dp_is1_js1;
47
48 for(int i = 0; i <= m; i++){
49 for(int j = 0; j <= n; j++){
50 int tmp = dp[j];
51 if(i == 0 || j == 0){
52 dp[j] = i+j;
53 }else if(word1[i-1] == word2[j-1]){
54 dp[j] = dp_is1_js1;
55 }else{
56 dp[j] = min(
57 dp[j], //remove word1[i-1]
58 dp[j-1] //remove word2[j-1]
59 ) + 1;
60 }
61 dp_is1_js1 = tmp;
62 }
63 }
64
65 return dp[n];
66 }
67};
68
69//Approach #3 Using Longest Common Subsequence- Dynamic Programming [Accepted]
70//https://leetcode.com/articles/delete-operation-for-two-strings/
71//time: O(m*n), space: O(m*n)
72class Solution {
73public:
74 int LCS(string word1, string word2){
75 int m = word1.size();
76 int n = word2.size();
77
78 vector<vector<int>> dp(m+1, vector(n+1, 0));
79
80 for(int i = 1; i <= m; i++){
81 for(int j = 1; j <= n; j++){
82 if(word1[i-1] == word2[j-1]){
83 dp[i][j] = dp[i-1][j-1] + 1;
84 }else{
85 dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
86 }
87 }
88 }
89
90 return dp[m][n];
91 };
92
93 int minDistance(string word1, string word2) {
94 int m = word1.size();
95 int n = word2.size();
96
97 return m + n - 2 * LCS(word1, word2);
98 }
99};
Cost