Check Vertical Reflection Symmetry with Duplicate Points
NuroSoftware Engineer · Technical ScreenLast updated
Determine whether a multiset of 2D points is symmetric across a vertical line, preserving duplicate counts and allowing half-integer axes.
Determine whether a multiset of two-dimensional points is symmetric about some vertical line. Every occurrence of a point must have a reflected occurrence, including duplicate counts.
Function Signature
has_vertical_symmetry(points: list[list[int]]) -> bool
Rules
A vertical reflection line has equation x = c, where c may be an integer or a half-integer. Reflecting (x, y) produces (2*c - x, y).
Return true if there exists such a line for which each coordinate pair and its reflection have equal multiplicity in the input. Points on the line reflect to themselves. Input order is irrelevant.
For this exercise, the empty multiset is symmetric and returns true.
Constraints
0 <= len(points) <= 200000.- Every point contains exactly two integer coordinates.
- Each coordinate is in
[-1000000000, 1000000000]. - Repeated points are allowed and their counts matter.
Examples
Input: points = [[1,2],[3,2],[2,4]]
Output: true
The line is x = 2.
Input: points = [[0,1],[1,1]]
Output: true
The line is x = 0.5.
Input: points = [[0,0],[0,0],[2,0]]
Output: false
The left point occurs twice while its reflected point occurs only once.
Input: points = []
Output: true
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.