Detect Squares LeetCode solution in Java

1
class DetectSquares {
2
List<int[]> coordinates;
3
Map<String, Integer> counts;
4

5
public DetectSquares() {
6
coordinates = new ArrayList<>();
7
counts = new HashMap<>();
8
}
9

10
public void add(int[] point) {
11
coordinates.add(point);
12
String key = point[0] + "@" + point[1];
13
counts.put(key, counts.getOrDefault(key, 0) + 1);
14
}
15

16
public int count(int[] point) {
17
int sum = 0, px = point[0], py = point[1];
18
for (int[] coordinate : coordinates) {
19
int x = coordinate[0], y = coordinate[1];
20
if (px == x || py == y || (Math.abs(px - x) != Math.abs(py - y))) continue;
21
sum += counts.getOrDefault(x + "@" + py, 0) * counts.getOrDefault(px + "@" + y, 0);
22
}
23

24
return sum;
25
}
26
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0