1402. Mirror Line Check
A tile designer has a pattern made of marked cells on an integer grid and wants to know whether it has mirror symmetry. Only mirrors parallel to an axis are considered: a vertical line x = c or a horizontal line y = c, where c may be a whole number or end in .5.
Treat points as a set, so duplicate entries are ignored. Return 0 if no axis-parallel mirror line maps the set onto itself, 1 if only a vertical line does, 2 if only a horizontal line does, and 3 if both do. A single point is symmetric about both lines. The only candidate vertical axis is the middle of the extremes, c = (minX + maxX) / 2, so work with doubled coordinates to avoid fractions: the mirror of (x, y) is (minX + maxX - x, y), and it must be present in the set. Do the same for horizontal with minY and maxY, using a hash set. The intended solution runs in O(n) expected time and O(n) space.
Example 1
- Input:
- points = [[0,0],[2,0],[1,3]]
- Output:
- 1
- Explanation:
The vertical line x = 1 swaps (0,0) and (2,0) and fixes (1,3), but no horizontal line works, so the answer is 1.
Example 2
- Input:
- points = [[0,0],[4,0],[0,2],[4,2]]
- Output:
- 3
- Explanation:
A rectangle is symmetric about both its vertical and its horizontal midline.
Constraints
1 ≤ points.length ≤ 1000
-108 ≤ x, y ≤ 108
Duplicate points may appear and count once
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n)
- Space
- O(n)