Medium

Quiz

#593 Valid Square

APPROACH

Given four 2D points p1, p2, p3, and p4 each as [x, y] (in no particular order), return true if they form a valid square — four equal-length positive sides meeting at right angles — or false otherwise.

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
1 of 4
1:00

What is the optimal approach for this problem?