package leetcode;

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

/**
 * https://leetcode.com/problems/number-of-boomerangs/
 */
public class Problem447 {
    public int numberOfBoomerangs(int[][] points) {
        Set<String> set = new HashSet<>();
        for (int i = 0; i < points.length; i++) {
            String key = points[i][0] + "|" + points[i][1];
            set.add(key);
        }
        int result = 0;
        for (int i = 0; i < points.length; i++) {
            int[] a = points[i];
            String keyA = a[0] + "|" + a[1];
            for (int j = 0; j < points.length; j++) {
                if (i == j) {
                    continue;
                }
                int[] b = points[j];
                String keyB = b[0] + "|" + b[1];
                int diffi = Math.abs(b[0] - a[0]);
                int diffj = Math.abs(b[1] - a[1]);

                Set<String> keys = new HashSet<>();

                String key1 = (a[0] + diffi) + "|" + (a[1] + diffj);
                keys.add(key1);
                if (!key1.equals(keyB) && !key1.equals(keyA)) {
                    if (set.contains(key1)) {
                        result++;
                    }
                }

                String key2 = (a[0] - diffi) + "|" + (a[1] - diffj);
                if (!keys.contains(key2)) {
                    keys.add(key2);
                    if (!key2.equals(keyB) && !key2.equals(keyA)) {
                        if (set.contains(key2)) {
                            result++;
                        }
                    }
                }

                String key3 = (a[0] + diffi) + "|" + (a[1] - diffj);
                if (!keys.contains(key3)) {
                    keys.add(key3);
                    if (!key3.equals(keyB) && !key3.equals(keyA)) {
                        if (set.contains(key3)) {
                            result++;
                        }
                    }
                }

                String key4 = (a[0] - diffi) + "|" + (a[1] + diffj);
                if (!keys.contains(key4)) {
                    keys.add(key4);
                    if (!key4.equals(keyB) && !key4.equals(keyA)) {
                        if (set.contains(key4)) {
                            result++;
                        }
                    }
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Problem447 prob = new Problem447();
        System.out.println(prob.numberOfBoomerangs(new int[][]{{0, 0}, {1, 0}, {2, 0}})); // 2
        System.out.println(prob.numberOfBoomerangs(new int[][]{{1, 4}, {0, 8}, {2, 8}, {4, 5}})); // 2
        System.out.println(prob.numberOfBoomerangs(new int[][]{{0, 0}, {1, 0}, {-1, 0}, {0, 1}, {0, -1}})); // 20
    }
}

@@ -16,57 +16,8 @@
 e;%0A%0A
-import java.util.HashSet;%0Aimport java.util.Set;%0A%0A
 /**%0A
@@ -165,662 +165,265 @@
     
-Set%3CString%3E set = new HashSet%3C%3E();%0A        for (int i = 0; i %3C points.length; i++) %7B%0A            String key = points%5Bi%5D%5B0%5D + %22%7C%22 + points%5Bi%5D%5B1%5D;%0A            set.add(key);%0A        %7D%0A        int result = 0;%0A        for (int i = 0; i %3C points.length; i++) %7B%0A            int%5B%5D a = points%5Bi%5D;%0A            String keyA = a%5B0%5D + %22%7C%22 + a%5B1%5D;%0A            for (int j = 0; j %3C points.length; j++) %7B%0A                if (i == j) %7B%0A                    continue;%0A                %7D%0A                int%5B%5D b = points%5Bj%5D;%0A                String keyB = b%5B0%5D + %22%7C%22 + b%5B1%5D;%0A                int diffi = Math.abs(b%5B0%5D - a%5B0%5D);%0A                int diffj = Math.abs(b%5B1%5D - a%5B1%5D
+int result = 0;%0A        for (int i = 0; i %3C points.length; i++) %7B%0A            for (int j = 0; j %3C points.length; j++) %7B%0A                if (i == j) %7B%0A                    continue;%0A                %7D%0A                int distance1 = getDistance(points, i, j
 );%0A
-%0A
     
@@ -438,206 +438,90 @@
     
-Set%3CString%3E keys = new HashSet%3C%3E();%0A%0A                String key1 = (a%5B0%5D + diffi) + %22%7C%22 + (a%5B1%5D + diffj);%0A                keys.add(key1);%0A                if (!key1.equals(keyB) && !key1.equals(keyA)
+for (int k = 0; k %3C points.length; k++) %7B%0A                    if (k == i %7C%7C k == j
 ) %7B%0A
@@ -544,380 +544,157 @@
     
-if (set.
+    
 cont
-a
 in
-s(key1)) %7B%0A                        result++;%0A                    %7D%0A                %7D%0A%0A                String key2 = (a%5B0%5D - diffi) + %22%7C%22 + (a%5B1%5D - diffj);%0A                if (!keys.contains(key2)) %7B%0A                    keys.add(key2);%0A                    if (!key2.equals(keyB) && !key2.equals(keyA)) %7B%0A                        if (set.contains(key2)) %7B%0A    
+ue;%0A                    %7D%0A                    int distance2 = getDistance(points, i, k);%0A                    if (distance1 == distance2) %7B%0A
     
@@ -739,32 +739,26 @@
             
-    %7D%0A  
+%7D%0A
             
@@ -753,34 +753,32 @@
                 
-  
 %7D%0A              
@@ -767,38 +767,33 @@
   %7D%0A            
-    %7D%0A
+%7D
 %0A               
@@ -781,32 +781,34 @@
   %7D%0A        
+%7D%0A
         
 String key3 
@@ -799,780 +799,219 @@
     
-String key3 = (a%5B0%5D + diffi) + %22%7C%22 + (a%5B1%5D - diffj)
+return result
 ;%0A    
+%7D%0A%0A
     
-        if (!keys.contains(key3)) %7B%0A                    keys.add(key3);%0A                    if (!key3.equals(keyB) && !key3.equals(keyA)) %7B%0A                        if (set.contains(key3)) %7B%0A                            result++;%0A                        %7D%0A                    %7D%0A                %7D%0A%0A                String key4 = (a%5B0%5D - diffi) + %22%7C%22 + (a%5B1%5D + diffj);%0A                if (!keys.contains(key4)) %7B%0A                    keys.add(key4);%0A                    if (!key4.equals(keyB) && !key4.equals(keyA)) %7B%0A                        if (set.contains(key4)) %7B%0A                            result++;%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D%0A        %7D%0A        return result
+private static int getDistance(int%5B%5D%5B%5D points, int i, int j) %7B%0A        int a = points%5Bi%5D%5B0%5D - points%5Bj%5D%5B0%5D;%0A        int b = points%5Bi%5D%5B1%5D - points%5Bj%5D%5B1%5D;%0A        return (a * a) + (b * b)
 ;%0A  

Update problem 447