Skip to content

Commit 6b06c8f

Browse files
dhruv-gupta14Razdeep
authored andcommitted
day 8 implementation in c++ (#95)
1 parent bfc5ecc commit 6b06c8f

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

day8/Cpp/day8.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}

day8/README.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,60 @@ function minEditDist (str1, str2) {
9797

9898
console.log(minEditDist ('abcdefgs', 'agced'));
9999
console.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+
```

0 commit comments

Comments
 (0)