Skip to content

Equal Row and Column Pairs

MediumArrays & Hashing
Decode it first. Clues, constraints, pattern and plan before any code.

Given an n x n integer grid, return the number of pairs (r, c) such that row r and column c are equal: they contain the same values in the same order (column read from top to bottom).

grid = [[3, 2, 1],
        [1, 7, 6],
        [2, 7, 7]]          →  1   (row 2 equals column 1: [2, 7, 7])
 
grid = [[1, 1],
        [1, 1]]             →  4   (every row equals every column)

Constraints

  • n == grid.length == grid[i].length
  • 1 <= n <= 200
  • 1 <= grid[i][j] <= 10^5