---
tags: leetcode
---
# [593. Valid Square](https://leetcode.com/problems/valid-square/)
---
# My Solution
## The Key Idea for Solving This Coding Question
## C++ Code
```cpp=
class Solution {
public:
bool validSquare(vector<int> &p1, vector<int> &p2, vector<int> &p3, vector<int> &p4) {
vector<vector<int>> points = {p1, p2, p3, p4};
return permutation(points, 0);
}
private:
bool permutation(vector<vector<int>> &points, int head) {
if (head == 4) {
return isSquare(points);
}
for (int i = head; i < 4; ++i) {
swap(points[head], points[i]);
if (permutation(points, i + 1)) {
return true;
}
swap(points[head], points[i]);
}
return false;
}
bool isSquare(vector<vector<int>> &points) {
int len1 = getLen(points[0][0], points[0][1], points[1][0], points[1][1]);
if (len1 == 0) {
return false;
}
int len2 = getLen(points[1][0], points[1][1], points[2][0], points[2][1]);
if (len2 == 0) {
return false;
}
int len3 = getLen(points[2][0], points[2][1], points[3][0], points[3][1]);
if (len3 == 0) {
return false;
}
int len4 = getLen(points[3][0], points[3][1], points[0][0], points[0][1]);
if (len4 == 0) {
return false;
}
int diagLen1 = getLen(points[0][0], points[0][1], points[2][0], points[2][1]);
if (diagLen1 == 0) {
return false;
}
int diagLen2 = getLen(points[1][0], points[1][1], points[3][0], points[3][1]);
if (diagLen1 == 0) {
return false;
}
return len1 == len2 && len2 == len3 && len3 == len4 && diagLen1 == diagLen2;
}
int getLen(int x1, int y1, int x2, int y2) {
return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}
};
```
## Time Complexity
## Space Complexity
# Miscellane
<!--
# Test Cases
```
[0,0]
[1,1]
[1,0]
[0,1]
```
```
[0,0]
[1,1]
[1,0]
[0,12]
```
```
[1,0]
[-1,0]
[0,1]
[0,-1]
```
```
[0,0]
[0,0]
[0,0]
[0,0]
```
```
[6987,-473]
[6985,-473]
[6986,-472]
[6986,-474]
```
-->