317. Equal Row and Column Pairs

Medium
Sequence Hashing
Hashing

Problem

Given an n by n integer matrix grid, return the number of pairs consisting of a row and a column that contain exactly the same values in exactly the same order.

Examples

Example 1

Input: [[3,2,1],[1,7,6],[2,7,7]]
Output: 1

Example 2

Input: [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]
Output: 3

Example 3

Input: [[1]]
Output: 1
Constraints

1 <= n <= 200

grid.length == grid[i].length == n

1 <= grid[i][j] <= 100000

Hints

?? Treat each complete row as a sequence key.

?? Count how many times every row sequence occurs.

?? Construct every column sequence and look it up in the row map.

Expected Complexity
Time: O(n^2)
Space: O(n^2)
Follow-up

Can you avoid repeatedly converting entire rows and columns into strings?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
Input
[[3,2,1],[1,7,6],[2,7,7]]
Expected
1