package leetcode;

import java.util.HashSet;
import java.util.Set;

/**
 * https://leetcode.com/problems/minimum-swaps-to-make-strings-equal/
 */
public class Problem1247 {
    public int minimumSwap(String s1, String s2) {
        Set<Integer> x1Set = new HashSet<>();
        Set<Integer> y1Set = new HashSet<>();
        Set<Integer> x2Set = new HashSet<>();
        Set<Integer> y2Set = new HashSet<>();
        for (int i = 0; i < s1.length(); i++) {
            if (s1.charAt(i) != s2.charAt(i)) {
                if (s1.charAt(i) == 'x') {
                    x1Set.add(i);
                    y2Set.add(i);
                } else {
                    y1Set.add(i);
                    x2Set.add(i);
                }
            }
        }
        int answer = 0;
        for (int i = 0; i < s1.length(); i++) {
            if (s1.charAt(i) != s2.charAt(i)) {
                if (s1.charAt(i) == 'x') {
                    if (x1Set.remove(i) && y2Set.remove(i)) {
                        if (!x1Set.isEmpty()) {
                            int index = x1Set.iterator().next();
                            x1Set.remove(index);
                            x2Set.remove(index);
                            y1Set.remove(index);
                            y2Set.remove(index);
                            answer++;
                        } else if (!x2Set.isEmpty()) {
                            int index = x2Set.iterator().next();
                            x1Set.remove(index);
                            x2Set.remove(index);
                            y1Set.remove(index);
                            y2Set.remove(index);
                            answer += 2;
                        } else {
                            return -1;
                        }
                    }
                } else {
                    if (y1Set.remove(i) && x2Set.remove(i)) {
                        if (!y1Set.isEmpty()) {
                            int index = y1Set.iterator().next();
                            x1Set.remove(index);
                            x2Set.remove(index);
                            y1Set.remove(index);
                            y2Set.remove(index);
                            answer++;
                        } else if (!y2Set.isEmpty()) {
                            int index = y2Set.iterator().next();
                            x1Set.remove(index);
                            x2Set.remove(index);
                            y1Set.remove(index);
                            y2Set.remove(index);
                            answer += 2;
                        } else {
                            return -1;
                        }
                    }
                }
            }
        }
        return answer;
    }

    public static void main(String[] args) {
        Problem1247 prob = new Problem1247();
        System.out.println(prob.minimumSwap("xx", "yy")); // 1
        System.out.println(prob.minimumSwap("xy", "yx")); // 2
        System.out.println(prob.minimumSwap("xx", "xy")); // -1
        System.out.println(prob.minimumSwap("xx", "xx")); // 0
        System.out.println(prob.minimumSwap("xxyyxyxyxx", "xyyxyxxxyx")); // 4
    }
}

@@ -1048,44 +1048,19 @@
     
-int index = x1Set.iterator().next();
+// Example:
 %0A   
@@ -1084,36 +1084,21 @@
         
-x1Set.remove(index);
+// xx
 %0A       
@@ -1118,36 +1118,108 @@
         
-x2Set.remove(index);
+// yy%0A                            int index = x1Set.iterator().next(); // pick any y from s1
 %0A       
@@ -1231,33 +1231,33 @@
                 
-y
+x
 1Set.remove(inde
@@ -1434,93 +1434,174 @@
     
-int index = x2Set.iterator().next();%0A                            x1Set.remove(index);
+// Example:%0A                            // xy%0A                            // yx%0A                            int index = x2Set.iterator().next(); // pick any x from s2
 %0A   
@@ -1699,57 +1699,8 @@
 x);%0A
-                            y2Set.remove(index);%0A
     
@@ -2023,44 +2023,53 @@
     
-int index = y1Set.iterator().next();
+// Example:%0A                            // yy
 %0A   
@@ -2093,36 +2093,21 @@
         
-x1Set.remove(index);
+// xx
 %0A       
@@ -2127,36 +2127,74 @@
         
-x2Set.remove(index);
+int index = y1Set.iterator().next(); // pick any y from s1
 %0A       
@@ -2255,33 +2255,33 @@
                 
-y
+x
 2Set.remove(inde
@@ -2409,44 +2409,53 @@
     
-int index = y2Set.iterator().next();
+// Example:%0A                            // yx
 %0A   
@@ -2479,36 +2479,21 @@
         
-x1Set.remove(index);
+// xy
 %0A       
@@ -2513,36 +2513,78 @@
         
-x2Set.remove(index);
+int index = y2Set.iterator().next(); // pick any y from the s2
 %0A       
@@ -2596,33 +2596,33 @@
                 
-y
+x
 1Set.remove(inde
@@ -2909,437 +2909,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1247 prob = new Problem1247();%0A        System.out.println(prob.minimumSwap(%22xx%22, %22yy%22)); // 1%0A        System.out.println(prob.minimumSwap(%22xy%22, %22yx%22)); // 2%0A        System.out.println(prob.minimumSwap(%22xx%22, %22xy%22)); // -1%0A        System.out.println(prob.minimumSwap(%22xx%22, %22xx%22)); // 0%0A        System.out.println(prob.minimumSwap(%22xxyyxyxyxx%22, %22xyyxyxxxyx%22)); // 4%0A    %7D
 %0A%7D%0A

Solve problem 1247