package leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * https://leetcode.com/problems/number-of-boomerangs/
 */
public class Problem447 {
    public int numberOfBoomerangs(int[][] points) {
        Map<String, Integer> distanceCounts = new HashMap<>();
        for (int i = 0; i < points.length; i++) {
            for (int j = 0; j < points.length; j++) {
                if (i == j) {
                    continue;
                }
                String key = i + "|" + getDistance(points, i, j);
                if (!distanceCounts.containsKey(key)) {
                    distanceCounts.put(key, 1);
                } else {
                    distanceCounts.put(key, distanceCounts.get(key) + 1);
                }
            }
        }
        int result = 0;
        for (int i = 0; i < points.length; i++) {
            for (int j = 0; j < points.length; j++) {
                if (i == j) {
                    continue;
                }
                int distance = getDistance(points, i, j);
                result += distanceCounts.get(i + "|" + distance) - 1;
            }
        }
        return result;
    }

    private static int getDistance(int[][] points, int i, int j) {
        int a = points[i][0] - points[j][0];
        int b = points[i][1] - points[j][1];
        return (a * a) + (b * b);
    }

    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
    }
}

@@ -214,61 +214,22 @@
     
-Map%3CString, Integer%3E distanceCounts = new HashMap%3C%3E()
+int result = 0
 ;%0A  
@@ -268,32 +268,100 @@
 .length; i++) %7B%0A
+            Map%3CInteger, Integer%3E distanceCounts = new HashMap%3C%3E();%0A
             for 
@@ -496,30 +496,22 @@
     
-String key = i + %22%7C%22 +
+int distance =
  get
@@ -582,19 +582,24 @@
 ainsKey(
-key
+distance
 )) %7B%0A   
@@ -634,19 +634,24 @@
 nts.put(
-key
+distance
 , 1);%0A  
@@ -712,19 +712,24 @@
 nts.put(
-key
+distance
 , distan
@@ -745,11 +745,16 @@
 get(
-key
+distance
 ) + 
@@ -801,342 +801,155 @@
     
-%7D%0A        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 distance = getDistance(points, i, j);%0A                result += distanceCounts.get(i + %22%7C%22 + distance
+    for (Map.Entry%3CInteger, Integer%3E entries : distanceCounts.entrySet()) %7B%0A                result += entries.getValue() * (entries.getValue(
 ) - 1
+)
 ;%0A  
@@ -1200,415 +1200,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem447 prob = new Problem447();%0A        System.out.println(prob.numberOfBoomerangs(new int%5B%5D%5B%5D%7B%7B0, 0%7D, %7B1, 0%7D, %7B2, 0%7D%7D)); // 2%0A        System.out.println(prob.numberOfBoomerangs(new int%5B%5D%5B%5D%7B%7B1, 4%7D, %7B0, 8%7D, %7B2, 8%7D, %7B4, 5%7D%7D)); // 2%0A        System.out.println(prob.numberOfBoomerangs(new int%5B%5D%5B%5D%7B%7B0, 0%7D, %7B1, 0%7D, %7B-1, 0%7D, %7B0, 1%7D, %7B0, -1%7D%7D)); // 20%0A    %7D
 %0A%7D%0A

Solve problem 447