Easy understand and fast python code


  • 1
    W
    class Solution(object):
        def isOneEditDistance(self, s, t):
            if len(s) > len(t):
                return self.isOneEditDistance(t, s)
            
            if len(t) - len(s) > 1:
                return False 
            
            cnt = 0
            if len(s) == len(t):
                for i in xrange(len(s)):
                    if s[i] != t[i]:
                        cnt += 1
                        if cnt > 1:
                            return False
                return cnt == 1
            else:
                i, j = 0, 0
                while i < len(s) and j < len(t):
                    if s[i] != t[j]:
                        cnt += 1
                        if cnt > 1:
                            return False
                        j += 1
                    else:
                        i += 1
                        j += 1
                return (cnt == 1 and i == len(s) and j == len(t)) or len(t) - j == 1

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.