FunctionRacerProblemsRace

Rows That Match Columns

MediumHash mapsPython, JavaScript, TypeScript, Java, C++

grid is a square grid of integers with n rows and n columns. Count the pairs (r, c) such that row r, read from left to right, is exactly the same sequence as column c, read from top to bottom.

Return the number of such pairs. Every row is compared with every column, so r == c is allowed, and a row that equals several columns counts once per column.

Example 1

grid [[3,2,1],[1,7,6],[2,7,7]]
returns 1

Row 2 is 2, 7, 7 and column 1 is 2, 7, 7; no other row equals a column.

Example 2

grid [[1,2],[2,1]]
returns 2

Row 0 equals column 0 and row 1 equals column 1.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Hash maps problems