<p>You are given <code>n</code> <code>points</code> in the plane that are all <strong>distinct</strong>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. A <strong>boomerang</strong> is a tuple of points <code>(i, j, k)</code> such that the distance between <code>i</code> and <code>j</code> equals the distance between <code>i</code> and <code>k</code> <strong>(the order of the tuple matters)</strong>.</p>

<p>Return <em>the number of boomerangs</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> points = [[0,0],[1,0],[2,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> points = [[1,1]]
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == points.length</code></li>
	<li><code>1 &lt;= n &lt;= 500</code></li>
	<li><code>points[i].length == 2</code></li>
	<li><code>-10<sup>4</sup> &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 10<sup>4</sup></code></li>
	<li>All the points are <strong>unique</strong>.</li>
</ul>

class Solution {
public:
    //int dp[505][505];
    int dis(vector<int> &a, vector<int> &b) {
        return pow(a[0]-b[0], 2)+pow(a[1]-b[1], 2);
    }
    // bool check(vector<vector<int>> &points, int i, int j, int k) {
    //     if(i == j || i == k || j ==k) return false;
    //     if (dp[i][j] == dp[i][k]) return true;
    //     return false;
    // }
    int numberOfBoomerangs(vector<vector<int>>& points) {
        int n = points.size();
        vector<unordered_map<int, int> > record(n, unordered_map<int,int>());
        for(int i = 0; i < n; i++) {
            for(int j = i+1; j < n;j++) {
                int d = dis(points[i], points[j]);
                if(record[i].find(d) == record[i].end()) record[i][d] = 0;
                if(record[j].find(d) == record[j].end()) record[j][d] = 0;
                record[i][d]++;
                record[j][d]++;
                //cout << i << " " << j << record[i][d] << " " << record[j][d] << endl;
            }
        }
        int res = 0;
        for(int i = 0; i < n; i++) {
            auto it = record[i].begin();
            while(it != record[i].end()) {
                if(it->second > 1) res += it->second*(it->second-1);
                it++;
            }
        }
        return res;
    }
};
83993