Python 32MS solution, beats 98%.


  • 0
    M
        def isOneEditDistance(self, s, t):
            """
            :type s: str
            :type t: str
            :rtype: bool
            """
            m = len(s)
            n = len(t)
            
            diff = abs(m - n)
            
            if diff > 1:
                return False
            
            if m < n:
                return self.isOneEditDistance(t, s)
                
            i = 0    
            while i < n and s[i] == t[i]:
                i = i + 1
            
            if diff == 0:
                i = i + 1
            
            while i < n and s[i + diff] == t[i]:
                i = i + 1
            
            return i == n
    

Log in to reply
 

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