# 0593. Valid Square

<https://leetcode.com/problems/valid-square>

## Description

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`

## ac

```java
class Solution {
    public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
        Map<Long, Integer> map = new HashMap<>();
        map.put(getDist(p1, p2), map.getOrDefault(getDist(p1, p2), 0) + 1);
        map.put(getDist(p1, p3), map.getOrDefault(getDist(p1, p3), 0) + 1);
        map.put(getDist(p1, p4), map.getOrDefault(getDist(p1, p4), 0) + 1);
        map.put(getDist(p2, p3), map.getOrDefault(getDist(p2, p3), 0) + 1);
        map.put(getDist(p2, p4), map.getOrDefault(getDist(p2, p4), 0) + 1);
        map.put(getDist(p3, p4), map.getOrDefault(getDist(p3, p4), 0) + 1);

        if (map.size() != 2) return false;

        long max = 0;
        for (long key : map.keySet()) {
            max = Math.max(max, key);
        }

        return map.get(max) == 2;
    }

    private long getDist(int[] p1, int[] p2) {
        return (long) Math.pow((p1[0] - p2[0]), 2) + (long) Math.pow((p1[1] - p2[1]), 2);
    }
}
// calculate 6 edges, and inspect them. square should have 2 max length, 4 edge length
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jaywin.gitbook.io/leetcode/solutions/0593-valid-square.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
