import collections
class Solution:
    def buddyStrings(self, A, B):
        if len(A) != len(B): return False
        c = collections.Counter
        a, b = c(A), c(B)
        diff = 0
        for i in range(0,len(A)):
            if A[i] != B[i]:
                diff+=1
        if diff == 2:
            return a == b
        elif diff == 0:
            return any(value >= 2 for value in a.values())
        else:
            return False

"""
class Solution(object):
    def buddyStrings(self, A, B):
        :type A: str
        :type B: str
        :rtype: bool
        def swap(i,j,str):
            s=str[0:i]+str[j]+str[i+1:j]+str[i]+str[j+1:]
            return s
        if len(A)<2:
            return False
        for i in range(len(A)):
            for j in range(i+1,len(A)):
                if swap(i,j,A)==B:
                    return True
        else:
            return False
"""
s = Solution()
A="ab"
B="ba"
print(s.buddyStrings(A,B))
