-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditDistance.cpp
More file actions
38 lines (36 loc) · 870 Bytes
/
Copy pathEditDistance.cpp
File metadata and controls
38 lines (36 loc) · 870 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>
#include<string>
using namespace std;
class Solution {
public:
int minDistance(string word1, string word2) {
int m=word1.length();
int n=word2.length();
int edit[m+1][n+1];
int i,j;
for(i=0;i<=m;i++)
edit[i][0]=i;
for(j=0;j<=n;j++)
edit[0][j]=j;
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(word1[i-1]==word2[j-1])
edit[i][j]=edit[i-1][j-1];
else
{
edit[i][j]=min(edit[i-1][j-1]+1,min(edit[i-1][j]+1,edit[i][j-1]+1));
}
}
}
return edit[m][n];
}
};
int main()
{
Solution s;
string s1="eeba";
string s2="abca";
cout<<s.minDistance(s1,s2)<<endl;
}