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.size(), m=word2.size();
- int dp[n+1][m+1];
- memset(dp, 0, sizeof dp);
- for(int i=0; i<=m; i++)
- dp[0][i] = i;
- for(int j=0; j<=n; j++)
- dp[j][0] = j;
- for(int i=1; i<=n; i++){
- for(int j=1; j<=m; j++){
- 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], dp[i-1][j-1]});
- }
- }
- return dp[n][m];
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment