Skip to content

Commit e0350cc

Browse files
committed
README.md of day8 updated
1 parent 5d38634 commit e0350cc

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

day8/C/levenshtein_distance.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @author: Rajdeep Roy Chowdhury<rrajdeeproychowdhury@gmail.com>
33
* @github: https://github.com/razdeep
4-
* @date: 24/12/2018
4+
* @date: 31/12/2018
55
*/
66
#include <stdio.h>
77
#include <string.h>

day8/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,66 @@ int main()
154154
return 0;
155155
}
156156
```
157+
158+
### C Implementation
159+
160+
### [Solution 1](./C/levenshtein_distance.c)
161+
162+
```c
163+
/**
164+
* @author: Rajdeep Roy Chowdhury<rrajdeeproychowdhury@gmail.com>
165+
* @github: https://github.com/razdeep
166+
* @date: 31/12/2018
167+
*/
168+
#include <stdio.h>
169+
#include <string.h>
170+
171+
int min(int x, int y, int z)
172+
{
173+
if (x <= y && x <= z)
174+
return x;
175+
else if (y <= x && y <= z)
176+
return y;
177+
else
178+
return z;
179+
}
180+
181+
int levenshtein_distance(const char *str1, const char *str2)
182+
{
183+
int m = strlen(str1);
184+
int n = strlen(str2);
185+
int ld[m + 1][n + 1];
186+
187+
for (int i = 0; i <= m; i++)
188+
{
189+
for (int j = 0; j <= n; j++)
190+
{
191+
if (i == 0)
192+
ld[i][j] = j;
193+
194+
else if (j == 0)
195+
ld[i][j] = i;
196+
197+
else if (str1[i - 1] == str2[j - 1])
198+
ld[i][j] = ld[i - 1][j - 1];
199+
200+
else
201+
ld[i][j] = 1 + min(ld[i][j - 1], ld[i - 1][j], ld[i - 1][j - 1]);
202+
}
203+
}
204+
205+
return ld[m][n];
206+
}
207+
208+
int main()
209+
{
210+
const int STRING_LENGTH=100;
211+
char str1[STRING_LENGTH], str2[STRING_LENGTH];
212+
scanf("%s%s",str1,str2);
213+
214+
printf("%d\n",levenshtein_distance(str1, str2));
215+
216+
return 0;
217+
}
218+
219+
```

0 commit comments

Comments
 (0)