Ctrl K

Rows That Match Columns

Medium
You are given an n × n matrix grid. Count the pairs (row r, column c) where row r and column c are identical: they hold the same numbers in the same order (a row is read left to right, a column top to bottom). One row can pair with several columns and vice versa; each pair counts separately.
Example 1
grid=[[2, 4], [4, 2]] → 2

Row 0, [2, 4], matches column 0, and row 1, [4, 2], matches column 1.

Example 2
grid=[[1, 2], [3, 4]] → 0

The columns are [1, 3] and [2, 4], and neither matches any row.

Example 3
grid=[[7, 7], [7, 7]] → 4

Every row and every column is [7, 7], so all 4 pairs count.

Constraints
  • 1 ≤ n ≤ 200
  • 1 ≤ grid[i][j] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNoughts and Crosses Verdict EasySudoku Board Without Clashes MediumSpiral Readout MediumSpiral-Numbered Square MediumZero Out the Crosses Medium
loading Python…
Input
grid=[[2, 4], [4, 2]]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 60 tests on the server, hidden ones included.