{x}
blog image

Valid Square

Given the coordinates of four points in 2D space p1, p2, p3 and p4, return true if the four points construct a square.

The coordinate of a point pi is represented as [xi, yi]. The input is not given in any order.

A valid square has four equal sides with positive length and four equal angles (90-degree angles).

 

Example 1:

Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
Output: true

Example 2:

Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,12]
Output: false

Example 3:

Input: p1 = [1,0], p2 = [-1,0], p3 = [0,1], p4 = [0,-1]
Output: true

 

Constraints:

  • p1.length == p2.length == p3.length == p4.length == 2
  • -104 <= xi, yi <= 104

Solution Explanation for Valid Square

The problem asks to determine if four given points in a 2D plane form a valid square. A valid square has four equal sides and four 90-degree angles. The points are given in an unordered manner.

Approach

The solution utilizes a helper function check to verify if three points can form two equal sides that are perpendicular to each other, forming a right angle. This forms the basis for checking the square condition. The main function then uses the check function multiple times, ensuring that all combinations of three points out of the four given points can form the necessary right angles with equal sides to create a square.

The check function calculates the squared Euclidean distances between the three input points (no need for square root as we only compare distances). If any combination of the three distances satisfies the condition that the sum of two distances equals the third distance (Pythagorean theorem for right-angled triangles) and that those two distances are equal (meaning equal sides), it implies a right angle with equal sides. The check function returns true if this condition is met, otherwise false.

The main function then calls the check function multiple times, checking all possible combinations of three points. If all calls return true, it confirms the four points form a square, otherwise, they do not form a square.

Time and Space Complexity

  • Time Complexity: O(1). The algorithm performs a fixed number of distance calculations and comparisons regardless of the input size. There are a constant number of combinations of three points to check (4 choose 3 = 4).

  • Space Complexity: O(1). The algorithm uses a constant amount of extra space to store variables for calculations, independent of the input size.

Code Explanation (Python)

class Solution:
    def validSquare(self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]) -> bool:
        def check(a, b, c):  # Helper function
            (x1, y1), (x2, y2), (x3, y3) = a, b, c
            d1 = (x1 - x2) ** 2 + (y1 - y2) ** 2  # Squared distance between a and b
            d2 = (x1 - x3) ** 2 + (y1 - y3) ** 2  # Squared distance between a and c
            d3 = (x2 - x3) ** 2 + (y2 - y3) ** 2  # Squared distance between b and c
 
            # Check for right-angled triangle with equal legs
            return any([
                d1 == d2 and d1 + d2 == d3 and d1 > 0,  # Condition 1: a-b and a-c are equal and form right angle
                d2 == d3 and d2 + d3 == d1 and d2 > 0,  # Condition 2: a-c and b-c are equal and form right angle
                d1 == d3 and d1 + d3 == d2 and d1 > 0   # Condition 3: a-b and b-c are equal and form right angle
            ])
 
        # Check all combinations of three points
        return (
            check(p1, p2, p3)
            and check(p2, p3, p4)
            and check(p1, p3, p4)
            and check(p1, p2, p4)
        )
 

The code in other languages (Java, C++, Go) follows the same logic and structure, with minor syntax differences. They all use the helper function check to efficiently verify the square condition by examining the distances between points and checking for right-angled triangles with equal legs.