-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path59.py
More file actions
24 lines (21 loc) · 662 Bytes
/
59.py
File metadata and controls
24 lines (21 loc) · 662 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution:
def equalPairs(self, grid: List[List[int]]) -> int:
cache = {}
n = len(grid)
for i in range(n):
hv = hash(tuple(grid[i]))
v = cache.get(hv, None)
if v is None:
cache[hv] = [1, 0]
else:
cache[hv] = [v[0] + 1, v[1]]
hv = hash(tuple([grid[j][i] for j in range(n)]))
v = cache.get(hv, None)
if v is None:
cache[hv] = [0, 1]
else:
cache[hv] = [v[0], v[1] + 1]
cnt = 0
for _, v in cache.items():
cnt += v[0] * v[1]
return cnt