-
Notifications
You must be signed in to change notification settings - Fork 0
/
60) EDIT Distance.cpp
54 lines (48 loc) · 1.35 KB
/
60) 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <bits/stdc++.h>
using namespace std;
class Solution{
public:
int editDistance(string s, string t){
//calculate the size of both the string
int n = s.size(), m = t.size();
//initialize two variables
int i = 0, j = 0;
//initialize array with size [N+1][M+1]
int arr[n+1][m+1];
//traverse over the first string
for(int i = 0;i<=n;i++){
//store the first string charecters in 0th place, 2nd palce will be empty
arr[i][0] = i; //
}
//similary for 2nd string
for(int i = 0;i<=m;i++){
arr[0][i] = i;
}
//Now check the each charecter
for(int i = 1;i<=n;i++){
//for string 1
for(int j = 1;j<=m;j++){
//for string 2
//check with if condition
if(s[i-1]== t[j-1]){
arr[i][j] = arr[i-1][j-1];
}else{
arr[i][j] = 1 + min(arr[i-1][j], min(arr[i][j-1], arr[i-1][j-1]));
}
}
}
return arr[n][m];
}
};
int main() {
int T;
cin >> T;
while (T--) {
string s, t;
cin >> s >> t;
Solution ob;
int ans = ob.editDistance(s, t);
cout << ans << "\n";
}
return 0;
}