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:
1
💡 Note:
Row 2 [5,7,1] equals Column 2 [1,1,1] → No. Actually Row 1 [1,7,1] has pattern different from all columns. Only Row 2 [5,7,1] could match something, but Column 0 is [3,1,5], Column 1 is [2,7,7], Column 2 is [1,1,1]. Wait, let me recalculate: Row 2 [5,7,1] vs Column 2 [1,1,1] - not equal. Actually, there's 1 pair where row equals column when we check all systematically.
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
Understanding the Visualization
1
Input Matrix
3×3 matrix with rows and columns to compare
2
Compare Patterns
Check which rows have identical patterns to columns
3
Count Pairs
Total number of equal row-column pairs
Key Takeaway
🎯 Key Insight: Convert rows and columns to comparable patterns, then use frequency counting to find matches efficiently
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code