Skip to content

Practice Detect Squares

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2013
LeetCode Problem

Detect Squares

Problem Statement

DetectSquares starts with no points. add([x,y]) records an occurrence; duplicates are distinct choices. count([x,y]) counts ways to select three stored occurrences that form a positive-area, axis-aligned square with the query point. The query itself need not have been added. Practice approach: Count occurrences by coordinate. For each possible opposite corner with equal nonzero horizontal and vertical distance, multiply its frequency by those of the two remaining corners. Sum these products. Do not count zero-area arrangements or multiply by stored copies of the supplied query point.

Source: Detect Squares on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["DetectSquares","add","add","add","count","add","count","count"]
[[],[[0,0]],[[2,0]],[[0,2]],[[2,2]],[[2,0]],[[2,2]],[[1,1]]]
Output
[null,null,null,null,1,null,2,0]
Explanation

Duplicating (2,0) doubles the number of choices for the square with query (2,2). No unit square exists for (1,1).

Constraints

Each point has two integer coordinates from 0 to 1000.
At most 3000 total add and count calls.

Related LeetCode Problems

Practice more problems similar to Detect Squares.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.