Skip to content

Commit 8e0b128

Browse files
committed
day 7
1 parent daed185 commit 8e0b128

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Delete Operation for Two Strings
3+
================================
4+
5+
Given two strings word1 and word2, return the minimum number of steps required to make word1 and word2 the same.
6+
7+
In one step, you can delete exactly one character in either string.
8+
9+
Example 1:
10+
Input: word1 = "sea", word2 = "eat"
11+
Output: 2
12+
Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
13+
14+
Example 2:
15+
Input: word1 = "leetcode", word2 = "etco"
16+
Output: 4
17+
18+
Constraints:
19+
1 <= word1.length, word2.length <= 500
20+
word1 and word2 consist of only lowercase English letters.
21+
*/
22+
23+
class Solution
24+
{
25+
public:
26+
int minDistance(string A, string B)
27+
{
28+
vector<vector<int>> dp(A.size() + 1, vector<int>(B.size() + 1, 0));
29+
for (int i = 1; i <= A.size(); ++i)
30+
{
31+
for (int j = 1; j <= B.size(); ++j)
32+
{
33+
if (A[i - 1] == B[j - 1])
34+
dp[i][j] = 1 + dp[i - 1][j - 1];
35+
else
36+
dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
37+
}
38+
}
39+
40+
return A.size() + B.size() - 2 * dp[A.size()][B.size()];
41+
}
42+
};

Leetcode Daily Challenge/May-2021/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
| 4. | [Non-decreasing Array](https://leetcode.com/explore/featured/card/may-leetcoding-challenge-2021/598/week-1-may-1st-may-7th/3731/) | [cpp](./04.%20Non-decreasing%20Array.cpp) |
99
| 5. | [Jump Game II](https://leetcode.com/explore/challenge/card/may-leetcoding-challenge-2021/598/week-1-may-1st-may-7th/3732/) | [cpp](./05.%20Jump%20Game%20II.cpp) |
1010
| 6. | [Convert Sorted List to Binary Search Tree](https://leetcode.com/explore/challenge/card/may-leetcoding-challenge-2021/598/week-1-may-1st-may-7th/3733/) | [cpp](./06.%20Convert%20Sorted%20List%20to%20Binary%20Search%20Tree.cpp) |
11+
| 7. | [Delete Operation for Two Strings](https://leetcode.com/explore/challenge/card/may-leetcoding-challenge-2021/598/week-1-may-1st-may-7th/3734/) | [cpp](./07.%20Delete%20Operation%20for%20Two%20Strings.cpp) |

0 commit comments

Comments
 (0)