107. Row Meets Column
A seating chart is stored as an n x n grid of integers grid. Read row r as the list of its n values from left to right, and column c as the list of its n values from top to bottom.
Count the pairs (r, c) for which row r and column c are identical lists, meaning they have equal values at every position. Every pair is counted separately, so a row that equals three different columns contributes three, and one column may equal several different rows.
Example 1
- Input:
- grid = [[2,5,2],[1,2,5],[2,5,2]]
- Output:
- 2
- Explanation:
Row 0 and row 2 are both [2,5,2], which equals column 2, so two pairs match.
Example 2
- Input:
- grid = [[1,2],[2,1]]
- Output:
- 2
- Explanation:
Row 0 equals column 0 and row 1 equals column 1, giving two pairs.
Example 3
- Input:
- grid = [[1,1],[1,1]]
- Output:
- 4
- Explanation:
Every row and every column is [1,1], so all four pairs match.
Constraints
n == grid.length == grid[i].length
1 ≤ n ≤ 1000
1 ≤ grid[i][j] ≤ 105
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 3,200 msC++ 800 msJava 1,600 msJavaScript 1,600 msTypeScript 1,600 ms
Expected complexity
- Time
- O(n^2)
- Space
- O(n^2)