class Solution {
public boolean buddyStrings(String s, String goal) {
if (s.length() != goal.length()) return false;
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < s.length; i++) {
if (s.charAt(i) != goal.charAt(i)) stack.push(i);
}
if (stack.size() == 0) {
int[] count = new int[26];
for (int i = 0; i < s.length(); i++) count[s.charAt(i)-'a'] += 1;
for (int cnt: count) if (count > 1) return true;
return false;
}
if (stack.size() != 2) return false;
int pos_1 = stack.pop(), pos_2 = stack.pop();
return s.charAt(pos_1) == goal.charAt(pos_2) && s.charAt(pos_2) == goal.charAt(pos_1);
}
}