1

So I am doing this question of EDIT DISTANCE and before going to DP approach I am trying to solve this question in recursive manner and I am facing some logical error, please help.... Here is my code -

class Solution {
public int minDistance(String word1, String word2) {
    int n=word1.length();
    int m=word2.length();
    if(m<n)
   return Solve(word1,word2,n,m);
     else
                return Solve(word2,word1,m,n);
}
private int Solve(String word1,String word2,int n,int m){
     if(n==0||m==0)
        return Math.abs(n-m);
    
    if(word1.charAt(n-1)==word2.charAt(m-1))
        return 0+Solve(word1,word2,n-1,m-1);
    
    else{
        //insert
        int insert = 1+Solve(word1,word2,n-1,m);
        
        //replace
        int replace = 1+Solve(word1,word2,n-1,m-1);
        
        //delete
        int delete = 1+Solve(word1,word2,n-1,m);
        
        int max1 = Math.min(insert,replace);
        return Math.min(max1,delete);
    }
}

}

here I am checking the last element of both the strings if both the characters are equal then simple moving both string to n-1 and m-1 resp. Else Now I am having 3 cases of insertion , deletion and replace ,and between these 3 I have to find minima. If I am replacing the character then simply I moved the character to n-1 & m-1. If I am inserting the character from my logic I think I should insert the character at the last of smaller length string and move the pointer to n-1 and m To delete the element I think I should delete the element from the larger length String that's why I move pointer to n-1 and m but I think I am making mistake here please help.

Leetcode is giving me wrong answer for word1 = "plasma" and word2 = "altruism".

2 Answers2

0

The problem is that the recursive expression for the insert-case is the same as for the delete-case.

Reasoning further, it turns out the one for the insert-case is wrong. In that case we choose to resolve the letter in word2 (at index m-1) through insertion, so it should not be considered any more during the recursive process. On the other hand the considered letter in word1 could still be matched with another letter in word2, so that letter should still be considered during the recursive process.

That means that m should be decremented, not n.

So change:

int insert = 1+Solve(word1,word2,n-1,m);

to:

int insert = 1+Solve(word1,word2,n,m-1);

...and it will work. Then remains to add the memoization for getting a good efficiency.

trincot
  • 317,000
  • 35
  • 244
  • 286
0

Python clean DP based solution,

class Solution:
    
    def minDistance(self, word1: str, word2: str) -> int:
        
        return self.edit_distance(word1, word2)
    
    
    @cache
    def edit_distance(self, s, t):
        
        # Edge conditions
        if len(s) == 0:
            return len(t)
        
        if len(t) == 0:
            return len(s)
        
        
        # If 1st char matches
        if s[0] == t[0]:
            return self.edit_distance(s[1:], t[1:])
        
        
        else:
            
            return min(
                1 + self.edit_distance(s[1:], t), # delete
                1 + self.edit_distance(s, t[1:]), # insert
                1 + self.edit_distance(s[1:], t[1:]) # replace
            )
Swapnil Masurekar
  • 458
  • 1
  • 9
  • 21