Skip to content

Commit 0c8a920

Browse files
authored
Create valid-square.py
1 parent 93f3f9e commit 0c8a920

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Python/valid-square.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Time: O(1)
2+
# Space: O(1)
3+
4+
# Given the coordinates of four points in 2D space,
5+
# return whether the four points could construct a square.
6+
#
7+
# The coordinate (x,y) of a point is represented by an integer array with two integers.
8+
#
9+
# Example:
10+
# Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
11+
# Output: True
12+
# Note:
13+
#
14+
# All the input integers are in the range [-10000, 10000].
15+
# A valid square has four equal sides with positive length
16+
# and four equal angles (90-degree angles).
17+
# Input points have no order.
18+
19+
class Solution(object):
20+
def validSquare(self, p1, p2, p3, p4):
21+
"""
22+
:type p1: List[int]
23+
:type p2: List[int]
24+
:type p3: List[int]
25+
:type p4: List[int]
26+
:rtype: bool
27+
"""
28+
def dist(p1, p2):
29+
return (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2
30+
31+
counter = collections.Counter([dist(p1, p2), dist(p1, p3),\
32+
dist(p1, p4), dist(p2, p3),\
33+
dist(p2, p4), dist(p3, p4)])
34+
return 0 not in counter and len(counter) == 2

0 commit comments

Comments
 (0)