<p>You are given a stream of points on the X-Y plane. Design an algorithm that:</p>

<ul>
	<li><strong>Adds</strong> new points from the stream into a data structure. <strong>Duplicate</strong> points are allowed and should be treated as different points.</li>
	<li>Given a query point, <strong>counts</strong> the number of ways to choose three points from the data structure such that the three points and the query point form an <strong>axis-aligned square</strong> with <strong>positive area</strong>.</li>
</ul>

<p>An <strong>axis-aligned square</strong> is a square whose edges are all the same length and are either parallel or perpendicular to the x-axis and y-axis.</p>

<p>Implement the <code>DetectSquares</code> class:</p>

<ul>
	<li><code>DetectSquares()</code> Initializes the object with an empty data structure.</li>
	<li><code>void add(int[] point)</code> Adds a new point <code>point = [x, y]</code> to the data structure.</li>
	<li><code>int count(int[] point)</code> Counts the number of ways to form <strong>axis-aligned squares</strong> with point <code>point = [x, y]</code> as described above.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/09/01/image.png" style="width: 869px; height: 504px;" />
<pre>
<strong>Input</strong>
[&quot;DetectSquares&quot;, &quot;add&quot;, &quot;add&quot;, &quot;add&quot;, &quot;count&quot;, &quot;count&quot;, &quot;add&quot;, &quot;count&quot;]
[[], [[3, 10]], [[11, 2]], [[3, 2]], [[11, 10]], [[14, 8]], [[11, 2]], [[11, 10]]]
<strong>Output</strong>
[null, null, null, null, 1, 0, null, 2]

<strong>Explanation</strong>
DetectSquares detectSquares = new DetectSquares();
detectSquares.add([3, 10]);
detectSquares.add([11, 2]);
detectSquares.add([3, 2]);
detectSquares.count([11, 10]); // return 1. You can choose:
                               //   - The first, second, and third points
detectSquares.count([14, 8]);  // return 0. The query point cannot form a square with any points in the data structure.
detectSquares.add([11, 2]);    // Adding duplicate points is allowed.
detectSquares.count([11, 10]); // return 2. You can choose:
                               //   - The first, second, and third points
                               //   - The first, third, and fourth points
</pre>

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

<ul>
	<li><code>point.length == 2</code></li>
	<li><code>0 &lt;= x, y &lt;= 1000</code></li>
	<li>At most <code>3000</code> calls <strong>in total</strong> will be made to <code>add</code> and <code>count</code>.</li>
</ul>

r'''
# Requirements

n = number of calls to add, m = number of calls to count
n + m <= 3000
0 <= x, y <= 1000
len(point) == 2

# Edge cases

1. Duplicate points
    - Duplicates are considered different
2. Squares of area zero
    - Only count squares with a positive area

# Solutions

1. Brute force, backtracking
    - add: O(1) time, O(1) auxiliary space
    - count: O(nC3 * n) time, O(n) auxiliary space

2. Hash map + hash set
    - add: O(1) time, O(1) auxiliary space
    - count: worst O(n) time, O(1) auxiliary space
'''
class DetectSquares:
    def __init__(self):
        self.freqs = defaultdict(int)
        self.points_by_x = defaultdict(list)

    def add(self, point: List[int]) -> None:
        x, y = point
        self.freqs[(x, y)] += 1
        self.points_by_x[x].append((x, y))

    def count(self, point: List[int]) -> int:
        x0, y0 = point
        ways = 0

        for _, y1 in self.points_by_x[x0]:
            # Discard points of the same coordinate to skip squares with an area of zero
            if y1 == y0:
                continue

            # Try to find points on both the Left and right
            dist = abs(y0 - y1)
            ways += self.freqs[(x0 - dist, y1)] * self.freqs[(x0 - dist, y0)]
            ways += self.freqs[(x0 + dist, y1)] * self.freqs[(x0 + dist, y0)]

        return ways


# Your DetectSquares object will be instantiated and called as such:
# obj = DetectSquares()
# obj.add(point)
# param_2 = obj.count(point)
349