1/22/2021

[LeetCode] 161. One Edit Distance

 Problem : https://leetcode.com/problems/one-edit-distance/



class Solution:
    def isOneEditDistance(self, s: str, t: str) -> bool:
        
        def helper(i, j, edit):
            if i == len(s):
                return j + edit == len(t)
            
            if j == len(t):
                return i + edit == len(s)
            
            if s[i] == t[j]:
                return helper(i+1, j+1, edit)
            else:
                if edit == 0:
                    return False
                
                # delete / insert / replace
                return helper(i+1, j, edit - 1) or helper(i, j + 1, edit - 1) or helper(i+1, j+1, edit-1)
        
        return helper(0, 0, 1)

No comments:

Post a Comment