-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditDistance.cpp
More file actions
27 lines (24 loc) · 774 Bytes
/
editDistance.cpp
File metadata and controls
27 lines (24 loc) · 774 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
int editDistance(string s, string t) {
int n = s.length(), m = t.length();
int dp[n+1][m+1];
for(int i=0; i<=n; i++){
for(int j=0; j<=m; j++){
if(i == 0 ){
dp[i][j] = j;
}
else if(j == 0){
dp[i][j] = i;
}
else if(s[i-1] == t[j-1]){
dp[i][j] = dp[i - 1][j - 1];
}
else {
dp[i][j] = 1+
min(dp[i][j- 1],
min(dp[i-1][j],
dp[i-1][j-1]));
}
}
}
return dp[n][m];
}