Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int minDistance(string word1, string word2) {
- int n = word1.length(), m = word2.length();
- int dp[n+1][m+1];
- memset(dp, 0, sizeof dp);
- for(int i=0; i<=n; i++)
- for(int j=0; j<=m; j++){
- if(i*j == 0)
- dp[i][j] = i+j;
- else if(word1[i-1] == word2[j-1])
- dp[i][j] = dp[i-1][j-1];
- else
- dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1]);
- }
- return dp[n][m];
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement