-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeetCode_edit-distance.cpp
More file actions
51 lines (40 loc) · 1.17 KB
/
NeetCode_edit-distance.cpp
File metadata and controls
51 lines (40 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// https://neetcode.io/problems/edit-distance?list=blind75
class Solution {
public:
int minDistance(string word1, string word2) {
int l1 = word1.length();
int l2 = word2.length();
vector<vector<int>> dp(l1+1, vector<int>(l2+1, -1));
int l = lcs2(word1, word2, l1, l2, dp);
return l;
}
int lcs2(string& word1, string& word2, int l1, int l2, vector<vector<int>>& dp)
{
if(l1 == 0 && l2 == 0)
return 0;
if(l1 == 1 && l2 == 1)
{
return word1[l1-1] == word2[l2-1] ? 0 : 1;
}
if(l2 == 0)
return l1;
if(l1 == 0)
return l2;
if(dp[l1][l2] != -1)
return dp[l1][l2];
int res = 0;
if(word1[l1-1] == word2[l2-1])
{
res = lcs2(word1, word2, l1-1, l2-1, dp);
}
else
{
int dp1 = 1+lcs2(word1, word2, l1 -1, l2, dp);
int dp2 = 1+lcs2(word1, word2, l1 -1, l2-1, dp);
int dp3 = 1+ lcs2(word1, word2, l1, l2-1, dp);
res = min(dp1, min(dp2, dp3));
}
dp[l1][l2] =res;
return res;
}
};