-
Notifications
You must be signed in to change notification settings - Fork 0
/
Edit Distance.cpp
34 lines (32 loc) · 1019 Bytes
/
Edit Distance.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
// Edit Distance
// Dynamic programming used.
int minDistance(string word1, string word2) {
int len1 = word1.length();
int len2 = word2.length();
if(len1 == 0){
return len2;
}
if(len2 == 0){
return len1;
}
vector<vector<int> > DP(len1+1, vector<int>(len2+1, 0));
for(int i = 1; i <= len1; i++){
DP[i][0] = i;
}
for(int j = 1; j <= len2; j++){
DP[0][j] = j;
}
for(int i = 1; i <= len1; i++){
for(int j = 1; j <= len2; j++){
//add a char to be the same; detele a char from A is the same as adding a char from B
DP[i][j] = min(DP[i-1][j]+1, DP[i][j-1]+1);
if(word1[i-1] == word2[j-1]){
DP[i][j] = min(DP[i-1][j-1], DP[i][j]); //if the last char is the same then we are done.
}
else{
DP[i][j] = min(DP[i-1][j-1]+1, DP[i][j]); //if the last char is not the same, we just substitute.
}
}
}
return DP[len1][len2];
}