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.

 

Example 1:

Input: A = "ab", B = "ba"
Output: true
Example 2:

Input: A = "ab", B = "ab"
Output: false
Example 3:

Input: A = "aa", B = "aa"
Output: true
Example 4:

Input: A = "aaaaaaabc", B = "aaaaaaacb"
Output: true
Example 5:

Input: A = "", B = "aa"
Output: false
 


Passed 20/23 cases 

class Solution {
    public boolean buddyStrings(String A, String B) {
        // iterate the string , the first time we see two letters are different, 
        // record the first index, and then if we another letter is different , record the second index
        // then swap these two index, 
        // and then check if the rest of the string is the same 
        
        // b - c 
        // c - b 
        // use a map, check if key 1 = value 2, key 2 = value 1
        if(A.length() != B.length()) return false;
        if(A.length() < 2) return false;
        char key = 'x';
        char value = 'x';
        Map<Character, Character> map = new HashMap<>();
        for(int i = 0; i < A.length(); i++){
            char charA = A.charAt(i);
            char charB = B.charAt(i);
            if(charA != charB){
                if(map.size() >= 2){
                    return false;
                }else if(map.size() == 0){
                    key = charA;
                    value = charB;
                    map.put(charA, charB);
                }else{
                    // map.size() == 1 
                    if(key != charB || value != charA){
                        return false;
                    }else{
                        map.put(charA, charB);
                    }
                }
            }
        }
        return map.size() == 2 || map.size() == 0;
    }
}



// Input:
// "ab"
// "ab"
// Output:
// true
// Expected:
// false