Given two strings s and goal, return true if you can swap two letters in s so the result is equal to goal, otherwise, return false.

Swapping letters is defined as taking two indices i and j (0-indexed) such that i != j and swapping the characters at s[i] and s[j].


	For example, swapping at indices 0 and 2 in "abcd" results in "cbad".


&nbsp;
Example 1:

Input: s = "ab", goal = "ba"
Output: true
Explanation: You can swap s[0] = 'a' and s[1] = 'b' to get "ba", which is equal to goal.


Example 2:

Input: s = "ab", goal = "ab"
Output: false
Explanation: The only letters you can swap are s[0] = 'a' and s[1] = 'b', which results in "ba" != goal.


Example 3:

Input: s = "aa", goal = "aa"
Output: true
Explanation: You can swap s[0] = 'a' and s[1] = 'a' to get "aa", which is equal to goal.


&nbsp;
Constraints:


	1 &lt;= s.length, goal.length &lt;= 2 * 104
	s and goal consist of lowercase letters.


from collections import Counter

class Solution:
    def buddyStrings(self, s: str, goal: str) -> bool:
        if len(s) != len(goal):
            return False
        diffCharactersCount = 0
        diffCharactersInS = []
        diffCharactersInGoal = []
        for i in range(len(s)):
            if s[i] != goal[i]:
                diffCharactersCount += 1
                diffCharactersInS.append(s[i])
                diffCharactersInGoal.append(goal[i])
        if diffCharactersCount == 2:
            # if there are only 2 different characters, then they should be swappable
            if ((diffCharactersInS[0] == diffCharactersInGoal[1]) and (diffCharactersInS[1] == diffCharactersInGoal[0])):
                return True
            return False
        elif diffCharactersCount == 0:
            # if there is atleast one repeating character in the string then its possible for swap
            counts = Counter(s)
            for k,v in counts.items():
                if v > 1:
                    return True
        # if different characters count is not 2 or 0, then it's not possible for the strings to be buddy strings
        return False
class Solution {
    public boolean buddyStrings(String s, String goal) {
        char a = '\u0000', b = '\u0000';
        char c = '\u0000', d = '\u0000';
        int lenS = s.length();
        int lenGoal = goal.length();
        boolean flag = true;
        HashSet<Character> hset = new HashSet<>();

        if(lenS != lenGoal)
            return false;

        if(s.equals(goal)){
            for(int i = 0; i < lenS; i++){
                if(!hset.contains(s.charAt(i))){
                    hset.add(s.charAt(i));
                }
                else
                    return true;
            }
            return false;
        }
        else{
            for(int i = 0; i < lenS; i++){
                if(s.charAt(i) == goal.charAt(i)){
                    continue;
                }
                if(a == '\u0000'){
                    a = s.charAt(i);
                    c = goal.charAt(i);
                    continue;
                }
                if(b == '\u0000'){
                    b = s.charAt(i);
                    d = goal.charAt(i);
                    continue;
                }
                return false;
            }

            if(a == d && c == b && a != '\u0000')
                return true;

            return false;
        }
    }
}
class Solution {
public:
    //In case string is duplicated, check if string has any duplicate letters
    bool dupeCase(string s){
        unordered_set<char> letters;
        for(auto it : s){
            if(letters.count(it)){  //Found dupe letter (they can be swapped)
                return true;
            } else {
                letters.insert(it);
            }
        }
        return false;   //all letters unique
    }
    
    bool buddyStrings(string s, string goal) {
        //check len
        if(goal.length() != s.length()) return false;
        //If strings are the same, use diff. method
        if(s == goal) return dupeCase(s);
        //Track index of differences 
        vector<int> diff;
        for(int i = 0; i < s.length(); i++){
            if(s[i] != goal[i]){    //If diff found
                if(diff.size() > 2){    //If this is third diff, return false
                    return false;
                } else {            //If not store in diff array
                    diff.push_back(i);
                }
            }
        }
        //If only one diff found, return false
        if(diff.size() == 1) return false;
        //else return if swap works
        return (s[diff[0]] == goal[diff[1]] && s[diff[1]] == goal[diff[0]]);
    }
};
var buddyStrings = function(A, B) {
    if(A.length != B.length) return false;
    const diff = [];
    
    for(let i = 0; i < A.length; i++) {
        if(A[i] != B[i]) diff.push(i);
        if(diff.length > 2) return false;
    }
    if(!diff.length) return A.length != [...new Set(A)].length;
    const [i, j] = diff; 
    return A[i] == B[j] && B[i] == A[j];
};
Buddy Strings