Home
DSA Patterns
Hash Map
Equal Row and Column Pairs
Equal Row and Column Pairs
Count the pairs of a row and a column in a square grid that hold the same values in the same order, by hashing every row and looking up every column.
Problem Understanding
Equal Row and Column Pairs (LeetCode 2352): given an n × n integer matrix grid, return the number of pairs (r, c) such that row r and column c contain the same elements in the same order.
Examples: [[3, 2, 1], [1, 7, 6], [2, 7, 7]] → 1 (row 2 and column 1 are both [2, 7, 7]); [[3, 1, 2, 2], [1, 4, 4, 5], [2, 4, 2, 2], [2, 4, 2, 2]] → 3.
The Intuition: A Row Is a Key
Comparing every column with every row is n² comparisons of n values each. A hash map turns the inner search into a lookup.
- Hash every row. Turn each row into a key and count it.
- Read every column. Build the same kind of key from the column.
- Look it up. The count stored under that key is the number of rows equal to this column, so add it.
Using a whole sequence as a key is the idea behind Group Anagrams, where the key is a word's letter count.
Interactive Walkthrough
4 × 4 GRID · ROWS HASHED
COUNT ROW
0
1
2
3
0
1
2
3
row counts
Distinct rows
1
Pairs
0
Answer
:
Row 0 is [3, 1, 2, 2]: count it, now 1
Next
Row 1 is [1, 4, 4, 5]: count it, now 1
grid
First each row is counted into the map on the right. Then each column is read top to bottom; the rows equal to it, and their key in the map, are outlined and turn green when their count is added. The default grid has a repeated row, so column 2 adds 2.
Dry Run Table
Input: [[3, 1, 2, 2], [1, 4, 4, 5], [2, 4, 2, 2], [2, 4, 2, 2]]. Row counts: [3, 1, 2, 2] → 1, [1, 4, 4, 5] → 1, [2, 4, 2, 2] → 2.
| Column | Values | Count in map | pairs |
|---|---|---|---|
| 0 | [3, 1, 2, 2] | 1 | 1 |
| 1 | [1, 4, 4, 4] | 0 | 1 |
| 2 | [2, 4, 2, 2] | 2 | 3 |
| 3 | [2, 5, 2, 2] | 0 | 3 |
Answer: 3.
The Solution Template
Count the rows by key, then add each column's count. The C version has no hash map to hand, so it compares each column with each row directly in O(n³).
Equal Row and Column Pairs Code
1var equalPairs = function(grid) {2 const n = grid.length;3 const rows = new Map();4 for (const row of grid) {5 const key = row.join(',');6 rows.set(key, (rows.get(key) ?? 0) + 1);7 }8 let pairs = 0;9 for (let c = 0; c < n; c++) {10 const col = grid.map((row) => row[c]).join(',');11 pairs += rows.get(col) ?? 0;12 }13 return pairs;14};
Edge Cases & Common Mistakes
- Repeated rows: store counts, not just presence; each equal row is a separate pair.
- Joining without a separator: "1" + "23" and "12" + "3" give the same string; join with a comma.
- Using a list as a Python dict key: lists are not hashable; convert to a tuple.
- 1 × 1 grid: the only row and column are the same cell, so the answer is 1.
- Symmetric grid: every column c equals row c, so the answer is at least n.
Summary
Count every row in a hash map keyed by its values, then build the same key for every column and add its count. O(n²) time and space, linear in the size of the grid.
More practice: DSA interview questions with answers · patterns cheat sheet
