Very simple python code


  • 0
    F
    class Solution(object):
    def isOneEditDistance(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        if len(s)==len(t): return len([ord(s[i])-ord(t[i]) for i in xrange(len(s)) if ord(s[i])!=ord(t[i])])==1
        if abs(len(s)-len(t))>=2: return False
        return self.cal(s,t) or self.cal(s[::-1],t[::-1])
        
    def cal(self, s, t):
        if len(s)>len(t):
            s, t= t, s
        if s[:len(s)]==t[:len(s)]: return True
        for i in xrange(len(s)):
            if s[i]!=t[i]:
                if s==t[:i]+t[i+1:]: return True
                else: return False

Log in to reply
 

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