Skip to content

Commit f06ad46

Browse files
committed
2
1 parent fafde08 commit f06ad46

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

EditDistance/EditDistance.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int minDistance(string word1, string word2) {
4+
// Start typing your C/C++ solution below
5+
// DO NOT write int main() function
6+
7+
int len1 = word1.size();
8+
int len2 = word2.size();
9+
10+
for (int i = 0; i <= max(len1, len2); i++)
11+
F[i][0] = F[0][i] = i;
12+
13+
for (int i = 1; i <= len1; i++) {
14+
for (int j = 1; j <= len2; j++) {
15+
if (word1[i-1] == word2[j-1])
16+
F[i][j] = F[i-1][j-1];
17+
else
18+
F[i][j] = min(F[i-1][j-1], min(F[i-1][j], F[i][j-1])) + 1;
19+
}
20+
}
21+
return F[len1][len2];
22+
}
23+
private:
24+
static const int MAXN = 1010;
25+
int F[MAXN][MAXN];
26+
};

0 commit comments

Comments
 (0)