-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditDistance.cpp
54 lines (51 loc) · 1.48 KB
/
EditDistance.cpp
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#define INF 1000000007
class Solution {
public:
int rec(string word1, string word2, int n1, int n2, int i, int j){
if(i==n1 && j==n2){
return 0;
}
if((i==n1 && j!=n2) ){
return n2-j;
}
if((i!=n1 && j==n2) ){
return n1-i;
}
int s=INF;
if(word1[i]==word2[j]){
s = min(s, rec(word1, word2, n1, n2, i+1, j+1));
}else{
s = min(s, rec(word1, word2, n1, n2, i+1, j+1)+1);
}
int v = min(rec(word1, word2, n1, n2, i+1, j), rec(word1, word2, n1, n2, i, j+1))+1;
s=min(s, v);
return s;
}
int dp(string word1, string word2, int n1, int n2){
int arr[n1+1][n2+1];
arr[n1][n2]=0;
for(int i=0; i<n1; i++){
arr[i][n2]=n1-i;
}
for(int j=0; j<n2; j++){
arr[n1][j]=n2-j;
}
for(int i=n1-1; i>=0; i--){
for(int j=n2-1; j>=0; j--){
if(word1[i]==word2[j]){
arr[i][j]=arr[i+1][j+1];
}else{
arr[i][j]=arr[i+1][j+1]+1;
}
arr[i][j]=min(arr[i][j], min(arr[i+1][j], arr[i][j+1])+1);
}
}
return arr[0][0];
}
int minDistance(string word1, string word2) {
int n1=word1.length();
int n2=word2.length();
//return rec(word1, word2, n1, n2, 0, 0);
return dp(word1, word2, n1, n2);
}
};