#2352
Equal Row and Column Pairs
pupil · 555 · lc medium +28 · verified · 70.8% accepted · 2,486 likes · top 80%
Description
Given a square integer matrix grid of size n x n (0-indexed), count and return the number of pairs (ri, cj) where row ri and column cj contain the same sequence of elements in the same order.
Example 1:
Input: grid = [[3,2,1],[1,7,6],[2,7,7]]
Output: 1
Explanation: There is 1 equal row and column pair:
- (Row 2, Column 1): [2,7,7]
Example 2:
Input: grid = [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]
Output: 3
Explanation: There are 3 equal row and column pairs:
- (Row 0, Column 0): [3,1,2,2]
- (Row 2, Column 2): [2,4,2,2]
- (Row 3, Column 2): [2,4,2,2]
Code
1
2
3