nikunjsoni

72

Jun 16th, 2021
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int minDistance(string word1, string word2) {
  4.         int n=word1.size(), m=word2.size();
  5.         int dp[n+1][m+1];
  6.         memset(dp, 0, sizeof dp);
  7.         for(int i=0; i<=m; i++)
  8.             dp[0][i] = i;
  9.         for(int j=0; j<=n; j++)
  10.             dp[j][0] = j;
  11.        
  12.         for(int i=1; i<=n; i++){
  13.             for(int j=1; j<=m; j++){
  14.                 if(word1[i-1] == word2[j-1])
  15.                     dp[i][j] = dp[i-1][j-1];
  16.                 else
  17.                     dp[i][j] = 1+min({dp[i-1][j], dp[i][j-1], dp[i-1][j-1]});
  18.             }
  19.         }
  20.         return dp[n][m];
  21.     }
  22. };
Advertisement
Add Comment
Please, Sign In to add comment