Skip to content

Latest commit

 

History

History
41 lines (38 loc) · 1.16 KB

593.md

File metadata and controls

41 lines (38 loc) · 1.16 KB

593. Valid Square

Solution 1 (time O(1), space O(1))

class Solution(object):
    def validSquare(self, p1, p2, p3, p4):
        """
        :type p1: List[int]
        :type p2: List[int]
        :type p3: List[int]
        :type p4: List[int]
        :rtype: bool
        """
        def check(p1, p2, p3, p4):
            if not p1[0] + p2[0] == p3[0] + p4[0]:
                return False
            if not p1[1] + p2[1] == p3[1] + p4[1]:
                return False
            v1 = (p1[0] - p2[0], p1[1] - p2[1])
            v2 = (p3[0] - p4[0], p3[1] - p4[1])
            if not v1[0] * v1[0] + v1[1] * v1[1] == v2[0] * v2[0] + v2[1] * v2[1]:
                return False
            if not v1[0] * v2[0] + v1[1] * v2[1] == 0:
                return False
            return True

        if p1 == p2:
            return False
        if check(p1, p2, p3, p4):
            return True
        if p1 == p3:
            return False
        if check(p1, p3, p2, p4):
            return True
        if p1 == p4:
            return False
        if check(p1, p4, p2, p3):
            return True
        return False