8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c71f611 commit 203211eCopy full SHA for 203211e
May-LeetCoding-Challenge/31-Edit-Distance/Edit-Distance.cpp
@@ -0,0 +1,18 @@
1
+class Solution {
2
+public:
3
+ int minDistance(string word1, string word2) {
4
+ int n = word1.size();
5
+ int m = word2.size();
6
+ vector<vector<int>> dp(n+1, vector<int>(m+1, 0));
7
+ for (int i = 1; i <= m; i++) dp[0][i] = i;
8
+ for (int i = 1; i <= n; i++) dp[i][0] = i;
9
+
10
+ for (int i = 1; i <= n; i++)
11
+ for (int j = 1; j <= m; j++){
12
+ dp[i][j] = min(dp[i-1][j-1], min(dp[i][j-1], dp[i-1][j])) + 1;
13
+ if (word1[i-1] == word2[j-1])
14
+ dp[i][j] = min(dp[i][j], dp[i-1][j-1]);
15
+ }
16
+ return dp[n][m];
17
18
+};
0 commit comments