Skip to content

Commit

Permalink
day 8 implementation in c++ (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruv-gupta14 authored and Razdeep committed Dec 31, 2018
1 parent bfc5ecc commit 6b06c8f
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
48 changes: 48 additions & 0 deletions day8/Cpp/day8.cpp
@@ -0,0 +1,48 @@
/*
* @author : dhruv-gupta14
* @date : 31/12/2018
*/

#include<bits/stdc++.h>
using namespace std;

int min(int x, int y, int z)
{
return min(min(x, y), z);
}

int levenshtein_distance(string str1, string str2, int m, int n)
{
int ld[m+1][n+1];

for (int i=0; i<=m; i++)
{
for (int j=0; j<=n; j++)
{
if (i==0)
ld[i][j] = j;

else if (j==0)
ld[i][j] = i;


else if (str1[i-1] == str2[j-1])
ld[i][j] = ld[i-1][j-1];

else
ld[i][j] = 1 + min(ld[i][j-1], ld[i-1][j], ld[i-1][j-1]);
}
}

return ld[m][n];
}

int main()
{
string str1,str2;
cin >> str1 >> str2;

cout << levenshtein_distance(str1, str2, str1.length(), str2.length());

return 0;
}
58 changes: 57 additions & 1 deletion day8/README.md
Expand Up @@ -97,4 +97,60 @@ function minEditDist (str1, str2) {

console.log(minEditDist ('abcdefgs', 'agced'));
console.log(minEditDist('kitten', 'sitting'));
```
```


### Cpp Implementation

### [Solution 1](./Cpp/day8.cpp)

```cpp
/*
* @author : dhruv-gupta14
* @date : 31/12/2018
*/

#include<bits/stdc++.h>
using namespace std;

int min(int x, int y, int z)
{
return min(min(x, y), z);
}

int levenshtein_distance(string str1, string str2, int m, int n)
{
int ld[m+1][n+1];

for (int i=0; i<=m; i++)
{
for (int j=0; j<=n; j++)
{
if (i==0)
ld[i][j] = j;

else if (j==0)
ld[i][j] = i;


else if (str1[i-1] == str2[j-1])
ld[i][j] = ld[i-1][j-1];

else
ld[i][j] = 1 + min(ld[i][j-1], ld[i-1][j], ld[i-1][j-1]);
}
}

return ld[m][n];
}

int main()
{
string str1,str2;
cin >> str1 >> str2;

cout << levenshtein_distance(str1, str2, str1.length(), str2.length());

return 0;
}
```

0 comments on commit 6b06c8f

Please sign in to comment.