Skip to content

Commit

Permalink
added code day8 (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
divyakhetan authored and MadhavBahl committed Jan 1, 2019
1 parent 73f78a6 commit 897e108
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
41 changes: 41 additions & 0 deletions day8/Cpp/EditDistanceday8.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @author:divyakhetan
* @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 main(){
string s1, s2;
cin >> s1 >> s2;

int m = s1.length();
int n = s2.length();
int edit[m + 1][n + 1];

for(int i = 0; i <= n; i++){
edit[0][i] = i;
}


for(int i = 0; i <= m; i++){
edit[i][0] = i;
}

for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
if(s1[i - 1] == s2[j - 1]) edit[i][j] = edit[i -1 ][j - 1];
else edit[i][j] = 1 + min(edit[ i -1][j], edit[i - 1][j - 1], edit[i][j - 1]);
}
}

cout << "min edit distance is " << edit[m][n];
return 0;
}
50 changes: 48 additions & 2 deletions day8/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![cover](./cover.png)
![cover](./cover.png)

# Day 8 - Maximum Edit Distance (Levenshtein Distance)

Expand Down Expand Up @@ -154,7 +154,53 @@ int main()
}
```
### [Solution 2] (./C++/levenshtein_distance.cpp)
### [Solution 2 by @divyakhetan](./Cpp/EditDistanceday8.cpp)
```cpp
/**
* @author:divyakhetan
* @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 main(){
string s1, s2;
cin >> s1 >> s2;
int m = s1.length();
int n = s2.length();
int edit[m + 1][n + 1];
for(int i = 0; i <= n; i++){
edit[0][i] = i;
}
for(int i = 0; i <= m; i++){
edit[i][0] = i;
}
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
if(s1[i - 1] == s2[j - 1]) edit[i][j] = edit[i -1 ][j - 1];
else edit[i][j] = 1 + min(edit[ i -1][j], edit[i - 1][j - 1], edit[i][j - 1]);
}
}
cout << "min edit distance is " << edit[m][n];
return 0;
}
```

### [Solution 3 by @aaditkamat] (./C++/levenshtein_distance.cpp)

```cpp
/**
Expand Down

0 comments on commit 897e108

Please sign in to comment.