SDE & MLE Prep

2352. Equal Row and Column Pairs

Problem

In the n×n matrix grid, count the pairs (row r, column c) where row r and column c contain the same values in the same order.

Example: [[3,2,1],[1,7,6],[2,7,7]] → 1 (row 2 equals column 1)

Constraints: n ≤ 200.

Hash every row as a tuple and count; then for each column (as a tuple) add how many rows equal it.

Complexity: O(n²).

class Solution:
    def equalPairs(self, grid: List[List[int]]) -> int:
        rows = Counter(tuple(r) for r in grid)
        return sum(rows[col] for col in zip(*grid))