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).

&nbsp;
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


&nbsp;
Constraints:


	p1.length == p2.length == p3.length == p4.length == 2
	-104 &lt;= xi, yi &lt;= 104


class Solution:
    def validSquare(self, p1, p2, p3, p4):

        def cal(A, B):
            return abs(A[0] - B[0]) + abs(A[1] - B[1])

        d = [cal(p1, p2), cal(p1, p3), cal(p1, p4), cal(p2, p3), cal(p2, p4), cal(p3, p4)]
        d.sort()

        return 0 < d[0] == d[1] == d[2] == d[3] and d[4] == d[5]
class Solution {
    // This method returns true if the given 4 points form a square, false otherwise
    public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
        // We use a set to store the distances between the points
        Set<Integer> set = new HashSet();
        // Calculate the distances between all pairs of points and add them to the set
        set.add(distanceSquare(p1,p2));
        set.add(distanceSquare(p1,p3));
        set.add(distanceSquare(p1,p4));
        set.add(distanceSquare(p2,p3));
        set.add(distanceSquare(p2,p4));
        set.add(distanceSquare(p3,p4));
        // A square must have 4 equal sides, so the set must contain 2 different values (the lengths of the sides and the diagonals)
        // The set should not contain 0, as that would mean that two points have the same coordinates
        return !set.contains(0) && set.size() == 2;
    }
    // This method calculates the distance between two points and returns its square
    private int distanceSquare(int[] a, int[] b){
        // We use the Pythagorean theorem to calculate the distance between the points
        return (a[0]-b[0])*(a[0]-b[0]) + (a[1]-b[1])*(a[1]-b[1]);
    }
}
class Solution {
public:
    bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
        vector<vector<int>> p{p1, p2, p3, p4};
        unsigned short ans{0};
        double scal;
        vector<double> bar(2);
        /* compute the barycenter */ 
        bar[0] = (p1[0] + p2[0] + p3[0] + p4[0]) / 4.;
        bar[1] = (p1[1] + p2[1] + p3[1] + p4[1]) / 4.;
        const double length = pow(p1[0]-bar[0],2) + pow(p1[1] - bar[1],2);
        for (size_t i=0; i<4; i++) {if ((pow(p[i][0]-bar[0], 2) + pow(p[i][1] - bar[1], 2)) != length) return false; 
            for (size_t j=i+1; j<4;j++){
                scal = (bar[0] - p[i][0])*(bar[0] - p[j][0]) + (bar[1] - p[i][1])*(bar[1]- p[j][1]);
                ans += (scal==0.)?1:0;}}
        return  ans==4;
    }
};
var validSquare = function(p1, p2, p3, p4) {
    const distance = (a, b) => {
        const [aX, aY] = a;
        const [bX, bY] = b;
        return (aX - bX) ** 2 + (aY - bY) ** 2;
    };

    const set = new Set([
        distance(p1, p2),
        distance(p1, p3),
        distance(p1, p4),
        distance(p2, p3),
        distance(p2, p4),
        distance(p3, p4),
    ]);

    return !set.has(0) && set.size === 2;
};
Valid Square