Skip to content

Instantly share code, notes, and snippets.

@bhaveshmunot1
Created June 4, 2020 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhaveshmunot1/cedf45bd050303e03a5e2616c3fdc42d to your computer and use it in GitHub Desktop.
Save bhaveshmunot1/cedf45bd050303e03a5e2616c3fdc42d to your computer and use it in GitHub Desktop.
Leetcode #72: Edit Distance
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];
}
};
@QureshiAbraham
Copy link

I found that solution very popular and helpful:
https://www.youtube.com/watch?v=YYo3PIclzjk&ab_channel=EricProgramming

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment