-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathlevenshtein_distance.c
55 lines (45 loc) · 1.08 KB
/
levenshtein_distance.c
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
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* @author: Rajdeep Roy Chowdhury<[email protected]>
* @github: https://github.com/razdeep
* @date: 31/12/2018
*/
#include <stdio.h>
#include <string.h>
int min(int x, int y, int z)
{
if (x <= y && x <= z)
return x;
else if (y <= x && y <= z)
return y;
else
return z;
}
int levenshtein_distance(const char *str1, const char *str2)
{
int m = strlen(str1);
int n = strlen(str2);
int ld[m + 1][n + 1];
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
if (i == 0)
ld[i][j] = j;
else if (j == 0)
ld[i][j] = i;
else if (str1[i - 1] == str2[j - 1])
ld[i][j] = ld[i - 1][j - 1];
else
ld[i][j] = 1 + min(ld[i][j - 1], ld[i - 1][j], ld[i - 1][j - 1]);
}
}
return ld[m][n];
}
int main()
{
const int STRING_LENGTH=100;
char str1[STRING_LENGTH], str2[STRING_LENGTH];
scanf("%s%s",str1,str2);
printf("%d\n",levenshtein_distance(str1, str2));
return 0;
}