less than 1 minute read

859. Buddy Strings (easy)

class Solution:
    def buddyStrings(self, s: str, goal: str) -> bool:
        n = len(s)
        if len(s) != len(goal):
            return False

        pair = []
        for i in range(n):
            if s[i] == goal[i]:
                continue
            pair.append((s[i], goal[i]))
        
        m = len(pair)
        if m == 2 and pair[0][1] == pair[1][0] and pair[0][0] == pair[1][1]:
            return True
        if m == 0:
            counter = Counter(goal)
            for v in counter.values():
                if v >= 2:
                    return True
            return False
        return False