-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path72. Edit Distance.cpp
42 lines (26 loc) · 929 Bytes
/
72. Edit Distance.cpp
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
class Solution {
int helper(const string &word1, int a, const string &word2, int b, vector<vector<int> > &dist) {
if (a < 0)
return b+1;
else if (b < 0)
return a+1;
if (dist[a][b] == -1) {
if (word1[a] == word2[b])
dist[a][b] = helper(word1, a-1, word2, b-1, dist);
else {
int sub = helper(word1, a-1, word2, b-1, dist);
int add = helper(word1, a-1, word2, b, dist);
int del = helper(word1, a, word2, b-1, dist);
dist[a][b] = 1 + min(min(sub,add),del);
}
}
return dist[a][b];
}
public:
int minDistance(string word1, string word2) {
int a = word1.length()-1;
int b = word2.length()-1;
vector<vector<int> > dist(a+1, vector<int> (b+1, -1));
return helper(word1, a, word2, b, dist);
}
};