161. One Edit Distance

Given two strings S and T, determine if they are both one edit distance apart.

Solution:

class Solution(object):
    def isOneEditDistance(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        if abs(len(s) - len(t)) > 1:
            return False
        for idx in xrange(min(len(s), len(t))):
            if s[idx] == t[idx]:
                continue
            if len(s) == len(t):
                return s[idx + 1:] == t[idx + 1:]
            if len(s) < len(t):
                return s[idx:] == t[idx + 1:]
            return s[idx + 1:] == t[idx:]
        return abs(len(s) - len(t)) == 1

results matching ""

    No results matching ""