Detect Squares Problem
Description
LeetCode Problem 2013.
You are given a stream of points on the X-Y plane. Design an algorithm that:
- Adds new points from the stream into a data structure. Duplicate points are allowed and should be treated as different points.
- Given a query point, counts the number of ways to choose three points from the data structure such that the three points and the query point form an axis-aligned square with positive area.
An axis-aligned square is a square whose edges are all the same length and are either parallel or perpendicular to the x-axis and y-axis.
Implement the DetectSquares class:
DetectSquares()Initializes the object with an empty data structure.void add(int[] point)Adds a new pointpoint = [x, y]to the data structure.int count(int[] point)Counts the number of ways to form axis-aligned squares with pointpoint = [x, y]as described above.
Example 1:

Input
["DetectSquares", "add", "add", "add", "count", "count", "add", "count"]
[[], [[3, 10]], [[11, 2]], [[3, 2]], [[11, 10]], [[14, 8]], [[11, 2]], [[11, 10]]]
Output
[null, null, null, null, 1, 0, null, 2]
Explanation
DetectSquares detectSquares = new DetectSquares();
detectSquares.add([3, 10]);
detectSquares.add([11, 2]);
detectSquares.add([3, 2]);
detectSquares.count([11, 10]); // return 1. You can choose:
// - The first, second, and third points
detectSquares.count([14, 8]); // return 0. The query point cannot form a square with any points in the data structure.
detectSquares.add([11, 2]); // Adding duplicate points is allowed.
detectSquares.count([11, 10]); // return 2. You can choose:
// - The first, second, and third points
// - The first, third, and fourth points
Constraints:
point.length == 20 <= x, y <= 1000- At most
3000calls in total will be made toaddandcount.
Solution
Approach: Hash Map of Point Counts
Store how many times each point was added: cnt[x][y]. Adding a point is a single increment.
To count squares for a query (x1, y1), fix the corner that lies on the same row, (x2, y1). The side length is d = x2 - x1 (with x2 != x1). The other two corners are then fixed by geometry: (x1, y1 + d) and (x2, y1 + d) for a square above, or (x1, y1 - d) and (x2, y1 - d) for one below. The number of squares for each choice is the product of the three counts, because duplicate points count as different points. Only x values that have been stored can serve as x2, so we loop over those keys and sum.
Complexity: add takes O(1) and count takes O(n) time, where n is the number of distinct x values stored; the space is O(n) for the points.
from collections import Counter, defaultdict
class DetectSquares:
def __init__(self):
self.cnt = defaultdict(Counter)
def add(self, point: List[int]) -> None:
x, y = point
self.cnt[x][y] += 1
def count(self, point: List[int]) -> int:
x1, y1 = point
if x1 not in self.cnt:
return 0
ans = 0
for x2 in self.cnt.keys():
if x2 != x1:
d = x2 - x1
ans += self.cnt[x2][y1] * self.cnt[x1][y1 + d] * self.cnt[x2][y1 + d]
ans += self.cnt[x2][y1] * self.cnt[x1][y1 - d] * self.cnt[x2][y1 - d]
return ans
# Your DetectSquares object will be instantiated and called as such:
# obj = DetectSquares()
# obj.add(point)
# param_2 = obj.count(point)