forked from royalpranjal/Interview-Bit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditDistance.cpp
More file actions
34 lines (29 loc) · 1.05 KB
/
Copy pathEditDistance.cpp
File metadata and controls
34 lines (29 loc) · 1.05 KB
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
// https://www.interviewbit.com/problems/edit-distance/
int Solution::minDistance(string A, string B) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
int row = A.size();
int col = B.size();
vector<vector<int> > temp(row+1, vector<int>(col+1));
for(int i = 0; i < temp.size(); i++){
for(int j = 0; j < temp[0].size(); j++){
if(j == 0){
temp[i][j] = i;
}
else if(i == 0){
temp[i][j] = j;
}
else if(A[i-1] == B[j-1]){
temp[i][j] = temp[i-1][j-1];
}
else{
temp[i][j] = min(temp[i-1][j-1], temp[i-1][j]);
temp[i][j] = min(temp[i][j-1], temp[i][j]);
temp[i][j] = temp[i][j]+1;
}
}
}
return temp[row][col];
}