class Solution:
# time O(N) space O(N)
def buddyStrings(self, A: str, B: str) -> bool:
#early termination: len_a != len_b
if len(A) != len(B):
return False
# case1: A == B
if A == B and len(A) > len(set(A)):
print('here')
return True
# case2: A != B, be careful there only allow to swap once
if A != B:
diff = [(a,b) for a,b in zip(A,B) if a != b]
# why need to compare each different char? e.g. "abcaa" "abcbb"
if len(diff) == 2 and diff[0][::-1] == diff[1]:
return True
return False