#2352

Equal Row and Column Pairs

Medium HashMap of row signatures

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

In an n×n grid, count pairs (row r, column c) where the row and the column contain the same values in the same order.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: count (row, column) pairs with identical values in order.

Why this works: turn each row into a key and count it; then build each column's key and look it up.

Example: [[3,2,1],[1,7,6],[2,7,7]]

column 1 = [2, 7, 7] equals row 2 → 1 pair

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Using int[] as a map key — arrays use identity, so equal rows don't match. Use Arrays.toString or a List.

Solve on LeetCode ↗
Complexity
  • Time: O(n²).
  • Space: O(n²).
Topic: Hashing
🎬 Episodes 37–39: video coming soon