> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Valid Square Python Solution with Tests

> Tested Python solution for LeetCode 593 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 593, [Medium](/catalog/medium). Topics: [Math](/catalog/topics/math), [Geometry](/catalog/topics/geometry). [View on LeetCode](https://leetcode.com/problems/valid-square/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 593   # by problem number
lcpy gen -s valid_square   # by problem name
```

## Problem

Given the coordinates of four points in 2D space \<code>p1\</code>, \<code>p2\</code>, \<code>p3\</code> and \<code>p4\</code>, return \<code>true\</code> \<em>if the four points construct a square\</em>.

\<p>The coordinate of a point \<code>p\<sub>i\</sub>\</code> is represented as \<code>\[x\<sub>i\</sub>, y\<sub>i\</sub>]\</code>. The input is \<strong>not\</strong> given in any order.\</p>

\<p>A \<strong>valid square\</strong> has four equal sides with positive length and four equal angles (90-degree angles).\</p>

### Examples

```
Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
Output: true
```

```
Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,12]
Output: false
```

```
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
* -10^4 \<= xi, yi \<= 10^4

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/valid_square/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/valid_square/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(1) - always 6 pairwise distances over 4 fixed points
    # Space: O(1)
    def valid_square(self, p1: list[int], p2: list[int], p3: list[int], p4: list[int]) -> bool:
        pts = (p1, p2, p3, p4)
        dists = sorted(
            (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2 for i, a in enumerate(pts) for b in pts[i + 1 :]
        )
        return 0 < dists[0] == dists[3] and dists[4] == dists[5] == 2 * dists[0]
```

## Complexity

| Time | Space |
| - | - |
| O(1) - always 6 pairwise distances over 4 fixed points | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.