Skip to content

Commit 6f8f8bc

Browse files
authored
Merge pull request #2801 from mdmzfzl/main
Create: 0072-edit-distance.c
2 parents 66882ce + 637d5e1 commit 6f8f8bc

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

c/0072-edit-distance.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
int min(int a, int b, int c) {
2+
return a < b ? (a < c ? a : c) : (b < c ? b : c);
3+
}
4+
5+
int minDistance(char *word1, char *word2) {
6+
int m = strlen(word1);
7+
int n = strlen(word2);
8+
9+
int dp[m + 1][n + 1];
10+
for (int i = 0; i <= m; ++i) {
11+
for (int j = 0; j <= n; ++j) {
12+
if (i == 0)
13+
dp[i][j] = j;
14+
else if (j == 0)
15+
dp[i][j] = i;
16+
else if (word1[i - 1] == word2[j - 1])
17+
dp[i][j] = dp[i - 1][j - 1];
18+
else
19+
dp[i][j] = 1 + min(dp[i - 1][j - 1], dp[i][j - 1], dp[i - 1][j]);
20+
}
21+
}
22+
23+
return dp[m][n];
24+
}

0 commit comments

Comments
 (0)