package leetcode;

/**
 * https://leetcode.com/problems/buddy-strings/
 */
public class Problem859 {
    public boolean buddyStrings(String A, String B) {
        if (A.length() != B.length()) {
            return false;
        }
        int idx1 = 0;
        int idx2 = 0;
        int count = 0;
        for (int i = 0; i < A.length(); i++) {
            if (A.charAt(i) != B.charAt(i)) {
                if (count == 0) {
                    idx1 = i;
                } else if (count == 1) {
                    idx2 = i;
                } else {
                    return false;
                }
                count++;
            }
        }
        return (A.charAt(idx1) == B.charAt(idx2) &&
            A.charAt(idx2) == B.charAt(idx1));
    }

    public static void main(String[] args) {
        Problem859 prob = new Problem859();
        System.out.println(prob.buddyStrings("ab", "ba")); // true
        System.out.println(prob.buddyStrings("ab", "ab")); // false
        System.out.println(prob.buddyStrings("aa", "aa")); // true
        System.out.println(prob.buddyStrings("aaaaaaabc", "aaaaaaacb")); // true
        System.out.println(prob.buddyStrings("", "aa")); // false
        System.out.println(prob.buddyStrings("abc", "cba")); // true
        System.out.println(prob.buddyStrings("aab", "aab")); // true
        System.out.println(prob.buddyStrings("aba", "aba")); // true
    }
}

@@ -303,46 +303,292 @@
     
-for (int i = 0; i %3C A.length(); i++) %7B
+int%5B%5D charCounts = new int%5B26%5D;%0A        boolean hasDuplicate = false;%0A        for (int i = 0; i %3C A.length(); i++) %7B%0A            int index = A.charAt(i) - 'a';%0A            charCounts%5Bindex%5D++;%0A            if (charCounts%5Bindex%5D %3E 1) %7B%0A                hasDuplicate = true;%0A            %7D
 %0A   
@@ -883,32 +883,101 @@
     %7D%0A        %7D%0A
+        if (count == 0) %7B%0A            return hasDuplicate;%0A        %7D%0A
         return (
@@ -1068,659 +1068,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem859 prob = new Problem859();%0A        System.out.println(prob.buddyStrings(%22ab%22, %22ba%22)); // true%0A        System.out.println(prob.buddyStrings(%22ab%22, %22ab%22)); // false%0A        System.out.println(prob.buddyStrings(%22aa%22, %22aa%22)); // true%0A        System.out.println(prob.buddyStrings(%22aaaaaaabc%22, %22aaaaaaacb%22)); // true%0A        System.out.println(prob.buddyStrings(%22%22, %22aa%22)); // false%0A        System.out.println(prob.buddyStrings(%22abc%22, %22cba%22)); // true%0A        System.out.println(prob.buddyStrings(%22aab%22, %22aab%22)); // true%0A        System.out.println(prob.buddyStrings(%22aba%22, %22aba%22)); // true%0A    %7D
 %0A%7D%0A

Solve problem 859