-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path72.cpp
More file actions
38 lines (34 loc) · 894 Bytes
/
Copy path72.cpp
File metadata and controls
38 lines (34 loc) · 894 Bytes
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
#include "common.h"
using namespace std;
class Solution {
public:
int minDistance(string word1, string word2) {
vector<vector<int>> v;
for (int i = 0; i < word1.size() + 1; ++i) {
v.push_back(vector<int>(word2.size() + 1, 0));
}
for (int i = 0; i <= word1.size(); ++i) {
v[i][0] = i;
}
for (int i = 0; i <= word2.size(); ++i) {
v[0][i] = i;
}
for (int i = 1; i <= word1.size(); ++i) {
for (int j = 1; j <= word2.size(); ++j) {
int min = std::min(v[i - 1][j], v[i][j - 1]) + 1;
if (word1[i - 1] == word2[j - 1]) {
min = std::min(min, v[i - 1][j - 1]);
} else {
min = std::min(min, v[i - 1][j - 1] + 1);
}
v[i][j] = min;
}
}
return v[word1.size()][word2.size()];
}
};
int main() {
Solution s;
cout << s.minDistance("horse", "ros") << endl;
return 0;
}