# 859.Buddy Strings

859.Buddy Strings

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
Note:
0 <= A.length <= 20000
0 <= B.length <= 20000
A and B consist only of lowercase letters.

class Solution {
public:
bool buddyStrings(string A, string B) {
if(A.length() != B.length() ) return false;
if(A==B)
{
vector<int>alpha(26,0);
for(auto c:A)
{
if(++alpha[c-'a'] >1) return true;
}
return false;
}
int count=0;
vector<int> index;
for(int i=0;i<A.length();i++)
{
if(A[i] !=B[i])
{
count++;
index.push_back(i);
}
}
if(count !=2) return false;
//cout<< index[0] <<" "<<index[1]<<endl;
if(A[index[0]] == B[index[1]] && A[index[1]] == B[index[0]]) return true;
return false;
}
};