593. Valid Square

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

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
```

Constraints:

• `p1.length == p2.length == p3.length == p4.length == 2`
• `-104 <= xi, yi <= 104`

593. Valid Square
``````struct Solution;

use std::collections::HashSet;

macro_rules! d {
(\$a:expr, \$b:expr) => {
(\$a[0] - \$b[0]) * (\$a[0] - \$b[0]) + (\$a[1] - \$b[1]) * (\$a[1] - \$b[1])
};
}

type Point = Vec<i32>;

impl Solution {
fn valid_square(p1: Point, p2: Point, p3: Point, p4: Point) -> bool {
let mut hs: HashSet<i32> = HashSet::new();
let v: Vec<Point> = vec![p1, p2, p3, p4];
for i in 0..4 {
for j in i + 1..4 {
hs.insert(d!(v[i], v[j]));
}
}
hs.len() == 2 && !hs.contains(&0)
}
}

#[test]
fn test() {
let p1 = vec![0, 0];
let p2 = vec![1, 1];
let p3 = vec![1, 0];
let p4 = vec![0, 1];
let res = true;
assert_eq!(Solution::valid_square(p1, p2, p3, p4), res);
let p1 = vec![0, 0];
let p2 = vec![1, 1];
let p3 = vec![0, 0];
let p4 = vec![0, 0];
let res = false;
assert_eq!(Solution::valid_square(p1, p2, p3, p4), res);
}
``````