Skip to content

Commit f76cb31

Browse files
authored
Create Day31.cpp
1 parent 821b88d commit f76cb31

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Day31.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Author: Aryan Yadav
3+
Edit Distance
4+
5+
Algorithm: DP
6+
Difficulty: Medium
7+
*/
8+
9+
class Solution {
10+
public:
11+
int minDistance(string word1, string word2) {
12+
int m = word1.length(), n = word2.length();
13+
int dp[m+1][n+1];
14+
15+
for(int i = 0; i <= m; i++){
16+
for(int j = 0; j <= n; j++){
17+
if (i == 0){
18+
dp[i][j] = j;
19+
} else if (j == 0){
20+
dp[i][j] = i;
21+
} else if (word1[i - 1] == word2[j - 1]){
22+
dp[i][j] = dp[i-1][j-1];
23+
} else {
24+
dp[i][j] = 1 + min(dp[i-1][j-1], min(dp[i][j-1], dp[i-1][j]));
25+
}
26+
}
27+
}
28+
return dp[m][n];
29+
}
30+
};

0 commit comments

Comments
 (0)