Leetcode 593 Solution

This article provides solution to leetcode question 593 (valid-square)

https://leetcode.com/problems/valid-square

Solution

class Solution:
    def dist(self, p1, p2):
        return (p2[1] - p1[1]) * (p2[1] - p1[1]) + (p2[0] - p1[0]) * (p2[0] - p1[0])

    def validSquare(self, p1: List[int], p2: List[int], p3: List[int], p4: List[int]) -> bool:
        points = sorted([tuple(p1), tuple(p2), tuple(p3), tuple(p4)])

        return self.dist(points[0], points[1]) != 0 and self.dist(points[0], points[1]) == self.dist(points[1], points[3]) == self.dist(points[3], points[2]) == self.dist(points[2], points[0]) \
            and self.dist(points[0], points[3]) == self.dist(points[1], points[2])