Advertisement
knakul853

Untitled

Jul 24th, 2020
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int minDistance(string word1, string word2) {
  4.         int l1 = word1.length();
  5.       int l2 = word2.length();
  6.       int dp[l1+1][l2+1];
  7.       for(int i=0;i<l1+1;i++)
  8.       {
  9.         for(int j=0;j<l2+1;j++)
  10.         {
  11.           if(i==0)
  12.           {
  13.             dp[i][j]=j;continue;
  14.           }
  15.           if(j==0)
  16.           {
  17.             dp[i][j]=i;continue;
  18.           }
  19.           if(word1[i-1]==word2[j-1])
  20.           {
  21.             dp[i][j]=dp[i-1][j-1];continue;
  22.           }
  23.          
  24.             dp[i][j]=1+min(dp[i-1][j],min(dp[i-1][j-1],dp[i][j-1]));
  25.          
  26.         }
  27.       }
  28.     return  dp[l1][l2];
  29.     }
  30. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement