Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
abdurrezzak authored Nov 26, 2017
1 parent 25ee928 commit 93a34d1
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions 27.EditDistance.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* This program takes two strings from the user and
* calculates Edit distance between them using
* Dynamic Prograrmming
*
* Coded by: Abdurrezak Efe
*
* */
#include <iostream>
#include <cmath>
using namespace std;

int edit_distance(string s1, string s2, int m, int n)
{

int d[m+1][n+1]; //our bottom up table

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

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

else if (s1[i-1] == s2[j-1]) //no op needed
d[i][j] = d[i-1][j-1];

else
d[i][j] = 1 + min(min(d[i][j-1], d[i-1][j]), d[i-1][j-1]); //insert delete
}
}

return d[m][n];
}

int main()
{
string s1,s2;

cin >> s1 >> s2;

cout << edit_distance(s1,s2,s1.size(),s2.size()) << endl;

}

0 comments on commit 93a34d1

Please sign in to comment.