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> xSet = new HashSet<>();
        Set<Integer> ySet = new HashSet<>();
        for (int i = 0; i < s1.length(); i++) {
            if (s1.charAt(i) != s2.charAt(i)) {
                if (s1.charAt(i) == 'x') {
                    xSet.add(i);
                } else {
                    ySet.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 (xSet.size() > 1) {
                        xSet.remove(i);
                        ySet.remove(i);
                        int index = xSet.iterator().next();
                        xSet.remove(index);
                        ySet.remove(index);
                        answer++;
                    }
                } else {
                    if (ySet.size() > 1) {
                        xSet.remove(i);
                        ySet.remove(i);
                        int index = ySet.iterator().next();
                        xSet.remove(index);
                        ySet.remove(index);
                        answer++;
                    }
                }
            }
        }
        return xSet.isEmpty() && ySet.isEmpty() ? answer : -1;
    }

    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("xxyyxyxyxx", "xyyxyxxxyx")); // 4
    }
}

@@ -239,16 +239,17 @@
 teger%3E x
+1
 Set = ne
@@ -285,16 +285,109 @@
 teger%3E y
+1Set = new HashSet%3C%3E();%0A        Set%3CInteger%3E x2Set = new HashSet%3C%3E();%0A        Set%3CInteger%3E y2
 Set = ne
@@ -557,24 +557,59 @@
            x
+1Set.add(i);%0A                    y2
 Set.add(i);%0A
@@ -650,24 +650,59 @@
            y
+1Set.add(i);%0A                    x2
 Set.add(i);%0A
@@ -935,51 +935,188 @@
 f (x
+1
 Set.
-size() %3E 1) %7B%0A                        x
+remove(i) && y2Set.remove(i)) %7B%0A                        if (!x1Set.isEmpty()) %7B%0A                            int index = x1Set.iterator().next();%0A                            x1
 Set.
@@ -1115,32 +1115,36 @@
   x1Set.remove(i
+ndex
 );%0A             
@@ -1146,33 +1146,87 @@
                 
-y
+    x2Set.remove(index);%0A                            y1
 Set.remove(i);%0A 
@@ -1213,35 +1213,185 @@
   y1Set.remove(i
+ndex
 );%0A
+                            y2Set.remove(index);%0A                            answer++;%0A                        %7D else if (!x2Set.isEmpty()) %7B%0A    
                 
@@ -1411,16 +1411,17 @@
 ndex = x
+2
 Set.iter
@@ -1451,33 +1451,38 @@
                 
+  
-x
+  x1
 Set.remove(index
@@ -1508,36 +1508,143 @@
         
-ySet.remove(index);%0A
+    x2Set.remove(index);%0A                            y1Set.remove(index);%0A                            y2Set.remove(index);%0A    
         
@@ -1657,36 +1657,137 @@
           answer
-++
+ += 2
 ;%0A
+                        %7D else %7B%0A                            return -1;%0A                        %7D%0A
                 
@@ -1846,51 +1846,188 @@
 f (y
+1
 Set.
-size() %3E 1) %7B%0A                        x
+remove(i) && x2Set.remove(i)) %7B%0A                        if (!y1Set.isEmpty()) %7B%0A                            int index = y1Set.iterator().next();%0A                            x1
 Set.
@@ -2026,32 +2026,36 @@
   x1Set.remove(i
+ndex
 );%0A             
@@ -2057,33 +2057,87 @@
                 
-y
+    x2Set.remove(index);%0A                            y1
 Set.remove(i);%0A 
@@ -2132,19 +2132,169 @@
 remove(i
+ndex
 );%0A
+                            y2Set.remove(index);%0A                            answer++;%0A                        %7D else if (!y2Set.isEmpty()) %7B%0A    
         
@@ -2322,16 +2322,17 @@
 ndex = y
+2
 Set.iter
@@ -2370,17 +2370,22 @@
         
+   
-x
+ x1
 Set.remo
@@ -2423,61 +2423,269 @@
     
-ySet.remove(index);%0A                        answer++;
+    x2Set.remove(index);%0A                            y1Set.remove(index);%0A                            y2Set.remove(index);%0A                            answer += 2;%0A                        %7D else %7B%0A                            return -1;%0A                        %7D
 %0A   
@@ -2763,55 +2763,15 @@
 turn
- xSet.isEmpty() && ySet.isEmpty() ?
  answer
- : -1
 ;%0A  
@@ -3056,16 +3056,79 @@
 ; // -1%0A
+        System.out.println(prob.minimumSwap(%22xx%22, %22xx%22)); // 0%0A
         

Update problem 1247