Created
June 4, 2020 03:49
-
-
Save bhaveshmunot1/cedf45bd050303e03a5e2616c3fdc42d to your computer and use it in GitHub Desktop.
Leetcode #72: Edit Distance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int minDistance(string word1, string word2) { | |
int n = word1.size(); | |
int m = word2.size(); | |
vector<vector<int>> dp(n+1, vector<int>(m+1)); | |
for (int i=0; i<=m; i++) { | |
dp[0][i] = i; | |
} | |
for (int i=0; i<=n; i++) { | |
dp[i][0] = i; | |
} | |
for (int i=1; i<=n; i++) { | |
for (int j=1; j<=m; j++) { | |
dp[i][j] = min( | |
{ | |
dp[i-1][j-1] + (word1[i-1] != word2[j-1]), | |
dp[i-1][j] + 1, | |
dp[i][j-1] + 1 | |
} | |
); | |
} | |
} | |
return dp[n][m]; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found that solution very popular and helpful:
https://www.youtube.com/watch?v=YYo3PIclzjk&ab_channel=EricProgramming