## The challenge

Given two strings `A` and `B` of lowercase letters, return `true` if you can swap two letters in `A` so the result is equal to `B`, 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 `A[i]` and `A[j]`. For example, swapping at indices `` and `2` in `"abcd"` results in `"cbad"`.

Example 1:

```Input: A = "ab", B = "ba"
Output: true
Explanation: You can swap A = 'a' and A = 'b' to get "ba", which is equal to B.```

Example 2:

```Input: A = "ab", B = "ab"
Output: false
Explanation: The only letters you can swap are A = 'a' and A = 'b', which results in "ba" != B.
```

Example 3:

```Input: A = "aa", B = "aa"
Output: true
Explanation: You can swap A = 'a' and A = 'a' to get "aa", which is equal to B.```

Example 4:

```Input: A = "aaaaaaabc", B = "aaaaaaacb"
Output: true
```

Example 5:

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

Constraints:

• `0 <= A.length <= 20000`
• `0 <= B.length <= 20000`
• `A` and `B` consist of lowercase letters.

## The solution in Java code

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 `````` ``````class Solution { public boolean buddyStrings(String A, String B) { if (A.length() != B.length() || A.isEmpty()) return false; if (!A.equals(B)) { int count = 0; char first = 'a', second = 'a'; for (int i = 0; i < A.length(); i++) { if (A.charAt(i) == B.charAt(i)) continue; if (count >= 2) return false; if (count == 0) { first = A.charAt(i); second = B.charAt(i); count++; continue; } if (A.charAt(i) != second || B.charAt(i) != first) return false; count++; } return count == 2; } if (A.length() > 26) return true; int [] fre = new int; for (char c : A.toCharArray()) { if (fre[c-'a'] == 1) return true; fre[c-'a']++; } return false; } } ``````