-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path72.cpp
More file actions
27 lines (21 loc) · 713 Bytes
/
Copy path72.cpp
File metadata and controls
27 lines (21 loc) · 713 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
class Solution {
public:
int minDistance(string word1, string word2) {
int s1 = word1.size(), s2 = word2.size();
if (s1 == 0) return s2;
if (s2 == 0) return s1;
vector<vector<int>> dp(s1+1, vector<int>(s2+1));
for (int i = 1; i <= s1; i++) dp[i][0] = i;
for (int j = 1; j <= s2; j++) dp[0][j] = j;
for (int i = 0; i < s1; i++) {
for (int j = 0; j < s2; j++) {
if (word1[i] == word2[j]) {
dp[i+1][j+1] = dp[i][j];
} else {
dp[i+1][j+1] = min(dp[i][j], min(dp[i+1][j], dp[i][j+1])) + 1;
}
}
}
return dp[s1][s2];
}
};