<jupyter_start><jupyter_text>Given two strings A and B of lowercase letters, return true if and only if we can swap two letters in A so that the result equals B.    Input: A = "ab", B = "ba"    Output: true<jupyter_code>from collections import defaultdict
def buddyStrings(A, B):
    if len(A)!=len(B) or len(A)<2:
        return False
    diff = []
    ac = defaultdict(int)
    for a,b in zip(A,B):
        ac[a]+=1
        if a!=b:
            diff.append(ord(a)-ord(b))
            if len(diff)>2:
                return False
    if sum(diff)!=0:
        return False
    elif not diff: # Identical strings
        if len(ac.values())>1 and min(ac.values())<2: # contains different characters but has an unique char
                                                    #(no swap possible)
            return False
        else:
            return True
    else:
        return True
buddyStrings('aa','aa')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10436634717784878, 'fraction_numerical': 0.006389776357827476, 'mean_word_length': 2.0031948881789137, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '137157', 'license': 'MIT', 'n_tokens_mistral': 283, 'n_tokens_neox': 279, 'n_words': 95, 'path': 'Buddy Strings.ipynb', 'repo_name': 'Lonitch/hackerRank', 'size': '939'}