@@ -8,9 +8,9 @@ Given two strings, and operations like replace, delete and add, write a program
8
8
9
9
## Example (source: [ Wikipedia] ( https://en.wikipedia.org/wiki/Levenshtein_distance ) )
10
10
11
- For example, the (Minimum Edit) Levenshtein distance between ` kitten ` and
12
- ` sitting ` is ` 3 ` , since the following three edits change one
13
- into the other, and there is no way to do it with fewer than
11
+ For example, the (Minimum Edit) Levenshtein distance between ` kitten ` and
12
+ ` sitting ` is ` 3 ` , since the following three edits change one
13
+ into the other, and there is no way to do it with fewer than
14
14
three edits:
15
15
16
16
1 . ** k** itten → ** s** itten (substitution of "s" for "k")
@@ -27,13 +27,13 @@ Mathematically, the Levenshtein distance between two strings `a` and `b` (of len
27
27
28
28
![ def] ( https://wikimedia.org/api/rest_v1/media/math/render/svg/f0a48ecfc9852c042382fdc33c19e11a16948e85 )
29
29
30
- where
30
+ where
31
31
![ def] ( https://wikimedia.org/api/rest_v1/media/math/render/svg/52512ede08444b13838c570ba4a3fc71d54dbce9 )
32
32
is the indicator function equal to ` 0 ` when
33
33
![ def] ( https://wikimedia.org/api/rest_v1/media/math/render/svg/231fda9ee578f0328c5ca28088d01928bb0aaaec )
34
34
and equal to 1 otherwise, and
35
35
![ def] ( https://wikimedia.org/api/rest_v1/media/math/render/svg/bdc0315678caad28648aafedb6ebafb16bd1655c )
36
- is the distance between the first ` i ` characters of ` a ` and the first
36
+ is the distance between the first ` i ` characters of ` a ` and the first
37
37
` j ` characters of ` b ` .
38
38
39
39
Therefore, the minimum edit distance between ` a ` and ` b ` is the last element in the edit distance matrix
@@ -109,50 +109,50 @@ console.log(minEditDist('kitten', 'sitting'));
109
109
* @date : 31/12/2018
110
110
*/
111
111
112
- #include < bits/stdc++.h>
113
- using namespace std ;
114
-
115
- int min (int x, int y, int z)
116
- {
117
- return min(min(x, y), z);
118
- }
119
-
120
- int levenshtein_distance(string str1, string str2, int m, int n)
121
- {
122
- int ld[ m+1] [ n+1 ] ;
123
-
124
- for (int i=0; i<=m; i++)
125
- {
126
- for (int j=0; j<=n; j++)
127
- {
128
- if (i==0)
112
+ #include < bits/stdc++.h>
113
+ using namespace std ;
114
+
115
+ int min (int x, int y, int z)
116
+ {
117
+ return min(min(x, y), z);
118
+ }
119
+
120
+ int levenshtein_distance(string str1, string str2, int m, int n)
121
+ {
122
+ int ld[ m+1] [ n+1 ] ;
123
+
124
+ for (int i=0; i<=m; i++)
125
+ {
126
+ for (int j=0; j<=n; j++)
127
+ {
128
+ if (i==0)
129
129
ld[i][j] = j;
130
-
131
- else if (j==0)
130
+
131
+ else if (j==0)
132
132
ld[i][j] = i;
133
-
134
-
135
- else if (str1[i-1] == str2[j-1])
136
- ld[i][j] = ld[i-1][j-1];
137
-
133
+
134
+
135
+ else if (str1[i-1] == str2[j-1])
136
+ ld[i][j] = ld[i-1][j-1];
137
+
138
138
else
139
- ld[i][j] = 1 + min(ld[i][j-1], ld[i-1][j], ld[i-1][j-1]);
140
- }
141
- }
142
-
143
- return ld[m][n];
144
- }
145
-
146
- int main()
147
- {
139
+ ld[i][j] = 1 + min(ld[i][j-1], ld[i-1][j], ld[i-1][j-1]);
140
+ }
141
+ }
142
+
143
+ return ld[m][n];
144
+ }
145
+
146
+ int main()
147
+ {
148
148
string str1,str2;
149
149
cin >> str1 >> str2;
150
-
151
- cout << levenshtein_distance(str1, str2, str1.length(), str2.length());
152
-
153
- return 0;
150
+
151
+ cout << levenshtein_distance(str1, str2, str1.length(), str2.length());
152
+
153
+ return 0;
154
154
}
155
- ```
155
+ ```
156
156
157
157
### [Solution 2 by @profgrammer](./Cpp/profgrammer_editdistance.cpp)
158
158
```cpp
@@ -351,6 +351,62 @@ int main()
351
351
return 0;
352
352
}
353
353
354
+ ```
355
+
356
+ ## Python Implementation
357
+
358
+ ### [Solution](./Python/minimum_edit_distance.py)
359
+
360
+ ```py
361
+
362
+ """
363
+ @author : imkaka
364
+ @date : 31/12/2018
365
+
366
+ """
367
+
368
+ import sys
369
+
370
+
371
+ def min_edit_distance(str1, str2):
372
+ len1 = len(str1)
373
+ len2 = len(str2)
374
+
375
+ # Matrix inilization
376
+ dp = [[0 for i in range(len2 + 1)]
377
+ for j in range(len1 + 1)]
378
+
379
+ for i in range(1, len1 + 1):
380
+ dp[i][0] = i
381
+
382
+ for j in range(1, len2 + 1):
383
+ dp[0][j] = j
384
+
385
+ # Fill the DP matrix.
386
+
387
+ for j in range(1, len2 + 1):
388
+ for i in range(1, len1 + 1):
389
+ if(str1[i - 1] == str2[j - 1]):
390
+ dp[i][j] = dp[i - 1][j - 1]
391
+ else:
392
+ dp[i][j] = 1 + min(dp[i - 1][j - 1],
393
+ dp[i][j - 1],
394
+ dp[i - 1][j])
395
+ return dp[len1][len2]
396
+
397
+
398
+ def main():
399
+
400
+ print("==================Minimum Edit Distance====================")
401
+ print()
402
+
403
+ print(min_edit_distance("kitten", "sitting"))
404
+ print(min_edit_distance("abcdef", "abcdhgikll"))
405
+
406
+
407
+ if __name__ == '__main__':
408
+ main()
409
+
354
410
```
355
411
356
412
## Java Implementation
@@ -415,6 +471,7 @@ public class Levenshtein {
415
471
System . out. println(" Minimum no of operations are " + dist);
416
472
}
417
473
}
474
+
418
475
```
419
476
420
477
### [ Solution 2] (./Java/LevenshteinDistance.java)
0 commit comments