File tree Expand file tree Collapse file tree 2 files changed +105
-1
lines changed
Expand file tree Collapse file tree 2 files changed +105
-1
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * @author : dhruv-gupta14
3+ * @date : 31/12/2018
4+ */
5+
6+ #include < bits/stdc++.h>
7+ using namespace std ;
8+
9+ int min (int x, int y, int z)
10+ {
11+ return min (min (x, y), z);
12+ }
13+
14+ int levenshtein_distance (string str1, string str2, int m, int n)
15+ {
16+ int ld[m+1 ][n+1 ];
17+
18+ for (int i=0 ; i<=m; i++)
19+ {
20+ for (int j=0 ; j<=n; j++)
21+ {
22+ if (i==0 )
23+ ld[i][j] = j;
24+
25+ else if (j==0 )
26+ ld[i][j] = i;
27+
28+
29+ else if (str1[i-1 ] == str2[j-1 ])
30+ ld[i][j] = ld[i-1 ][j-1 ];
31+
32+ else
33+ ld[i][j] = 1 + min (ld[i][j-1 ], ld[i-1 ][j], ld[i-1 ][j-1 ]);
34+ }
35+ }
36+
37+ return ld[m][n];
38+ }
39+
40+ int main ()
41+ {
42+ string str1,str2;
43+ cin >> str1 >> str2;
44+
45+ cout << levenshtein_distance (str1, str2, str1.length (), str2.length ());
46+
47+ return 0 ;
48+ }
Original file line number Diff line number Diff line change @@ -97,4 +97,60 @@ function minEditDist (str1, str2) {
9797
9898console .log (minEditDist (' abcdefgs' , ' agced' ));
9999console .log (minEditDist (' kitten' , ' sitting' ));
100- ```
100+ ```
101+
102+
103+ ### Cpp Implementation
104+
105+ ### [ Solution 1] ( ./Cpp/day8.cpp )
106+
107+ ``` cpp
108+ /*
109+ * @author : dhruv-gupta14
110+ * @date : 31/12/2018
111+ */
112+
113+ #include < bits/stdc++.h>
114+ using namespace std ;
115+
116+ int min (int x, int y, int z)
117+ {
118+ return min(min(x, y), z);
119+ }
120+
121+ int levenshtein_distance(string str1, string str2, int m, int n)
122+ {
123+ int ld[ m+1] [ n+1 ] ;
124+
125+ for (int i=0; i<=m; i++)
126+ {
127+ for (int j=0; j<=n; j++)
128+ {
129+ if (i==0)
130+ ld[i][j] = j;
131+
132+ else if (j==0)
133+ ld[i][j] = i;
134+
135+
136+ else if (str1[i-1] == str2[j-1])
137+ ld[i][j] = ld[i-1][j-1];
138+
139+ else
140+ ld[i][j] = 1 + min(ld[i][j-1], ld[i-1][j], ld[i-1][j-1]);
141+ }
142+ }
143+
144+ return ld[m][n];
145+ }
146+
147+ int main()
148+ {
149+ string str1,str2;
150+ cin >> str1 >> str2;
151+
152+ cout << levenshtein_distance(str1, str2, str1.length(), str2.length());
153+
154+ return 0;
155+ }
156+ ```
You can’t perform that action at this time.
0 commit comments