Equal Row and Column Pairs - Problem
Given a 0-indexed n x n integer matrix grid, return the number of pairs (ri, cj) such that row ri and column cj are equal.
A row and column pair is considered equal if they contain the same elements in the same order (i.e., an equal array).
Input & Output
Example 1 — Basic Case
$
Input:
grid = [[3,2,1],[1,7,1],[5,7,1]]
›
Output:
0
💡 Note:
We check all row-column pairs: Row 0 [3,2,1] vs Columns [3,1,5], [2,7,7], [1,1,1] - no matches. Row 1 [1,7,1] vs Columns - no matches. Row 2 [5,7,1] vs Columns - no matches. Total: 0 pairs.
Example 2 — Multiple Matches
$
Input:
grid = [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]
›
Output:
3
💡 Note:
After checking all row-column pairs systematically, we find 3 pairs where the row pattern exactly matches the column pattern.
Example 3 — Single Element
$
Input:
grid = [[5]]
›
Output:
1
💡 Note:
Only one row [5] and one column [5], they are equal, so 1 pair.
Constraints
- n == grid.length == grid[i].length
- 1 ≤ n ≤ 200
- 1 ≤ grid[i][j] ≤ 105
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code