-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditDistance.cpp
More file actions
36 lines (30 loc) · 1 KB
/
editDistance.cpp
File metadata and controls
36 lines (30 loc) · 1 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
35
36
class Solution {
public:
int minDistance(string word1, string word2) {
int m = word1.length();
int n = word2.length();
if(!m && !n) return 0;
if(!m) return n;
if(!n) return m;
vector<vector< int> > myDP(m+1,vector<int>(n+1,0));
// //Initial Values for when second word is zero
for(int i =1; i<=m; i++ ){
myDP[i][0] = i;
}
// //Initial Values for when first word is zero
for(int i =1; i<=n; i++ ){
myDP[0][i] = i;
}
for(int i =1; i<=m; i++){
for(int j=1; j<=n; j++){
if(word1[i-1] == word2[j-1]){
myDP[i][j] = myDP[i-1][j-1];
}
else{
myDP[i][j] = min(myDP[i-1][j-1] + 1,min( myDP[i-1][j]+1, myDP[i][j-1]+1));
}
}
}
return myDP[m][n];
}
};