Browse curriculum

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.

  1. Hash every row. Turn each row into a key and count it.
  2. Read every column. Build the same kind of key from the column.
  3. 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

3
1
2
2

1

1
4
4
5

2

2
4
2
2

3

2
4
2
2

row counts

[3, 1, 2, 2] → 1

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

Free preview: the first 4 of 13 steps

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

The approach, step by step

  1. Hash the rows

    Count each row, as a tuple or string key, in a hash map.

  2. Read each column

    Build the same kind of key from each column, top to bottom.

  3. Add the matches

    Add the column key's count from the map to pairs, and return pairs.

Frequently asked questions

Why store a count for each row instead of a set?

Every equal row forms its own pair with a column. If two rows are both [2, 4, 2, 2] and a column is [2, 4, 2, 2], that is 2 pairs, so the map has to remember how many times each row occurs.


What is the time complexity?

Building a key from a row or column of n values costs O(n), and there are 2n of them, so O(n²) in total, which is linear in the size of the grid. Comparing every column with every row directly is O(n³).


How do you use a row as a hash map key?

Use a hashable form of the whole row: a tuple in Python, a string such as "2,4,2,2" in Java or JavaScript, or the vector itself as a key of std::map in C++. Use a separator in strings, or [1, 23] and [12, 3] would collide.


Is there a way without a hash map?

Yes: compare each column with each row element by element, as the C version does. It needs no extra memory but takes O(n³) time; at LeetCode's limit of n = 200 that is still fast enough.

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description