@@ -8,9 +8,9 @@ Given two strings, and operations like replace, delete and add, write a program
88
99## Example (source: [ Wikipedia] ( https://en.wikipedia.org/wiki/Levenshtein_distance ) )
1010
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
1414three edits:
1515
16161 . ** 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
2727
2828![ def] ( https://wikimedia.org/api/rest_v1/media/math/render/svg/f0a48ecfc9852c042382fdc33c19e11a16948e85 )
2929
30- where
30+ where
3131![ def] ( https://wikimedia.org/api/rest_v1/media/math/render/svg/52512ede08444b13838c570ba4a3fc71d54dbce9 )
3232is the indicator function equal to ` 0 ` when
3333![ def] ( https://wikimedia.org/api/rest_v1/media/math/render/svg/231fda9ee578f0328c5ca28088d01928bb0aaaec )
3434and equal to 1 otherwise, and
3535![ 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
3737` j ` characters of ` b ` .
3838
3939Therefore, 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'));
109109* @date : 31/12/2018
110110*/
111111
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)
129129 ld[i][j] = j;
130-
131- else if (j==0)
130+
131+ else if (j==0)
132132 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+
138138 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+ {
148148 string str1,str2;
149149 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;
154154}
155- ```
155+ ```
156156
157157### [Solution 2 by @profgrammer](./Cpp/profgrammer_editdistance.cpp)
158158```cpp
@@ -351,6 +351,62 @@ int main()
351351 return 0;
352352}
353353
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+
354410```
355411
356412## Java Implementation
@@ -415,6 +471,7 @@ public class Levenshtein {
415471 System . out. println(" Minimum no of operations are " + dist);
416472 }
417473}
474+
418475```
419476
420477### [ Solution 2] (./Java/LevenshteinDistance.java)
0 commit comments