1. class Solution {
    2. public:
    3. int minDistance(string word1, string word2) {
    4. int l1 = word1.size();
    5. int l2 = word2.size();
    6. if(l1 <= 0){
    7. return l2;
    8. }
    9. if(l2 <= 0){
    10. return l1;
    11. }
    12. vector<vector<int> > dp(l1 + 1, vector<int>(l2 + 1, 0));
    13. // dp[0][0] = word1[0] == word1[1] ? 0: 1;
    14. for (int i = 0; i <=l1; i++)
    15. {
    16. dp[i][0] = i;
    17. }
    18. for (int j = 0; j <=l2; j++)
    19. {
    20. dp[0][j] = j;
    21. }
    22. for(int i = 1; i<=l1;i++){
    23. for(int j=1; j<=l2; j++){
    24. if(word1[i - 1] == word2[j-1]){
    25. dp[i][j] = dp[i - 1][j - 1];
    26. }else{
    27. dp[i][j] = min(dp[i - 1][j - 1], min(dp[i][j - 1] , dp[i - 1][j])) + 1;
    28. }
    29. }
    30. }
    31. return dp[l1][l2];
    32. }
    33. };