-
Notifications
You must be signed in to change notification settings - Fork 0
/
equal_row_and_column_paird.py
56 lines (40 loc) · 1.5 KB
/
equal_row_and_column_paird.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Given a 0-indexed n x n integer matrix grid, return the number of pairs (Ri, Cj) such that row Ri and column Cj are equal.
# A row and column pair is considered equal if they contain the same elements in the same order (i.e. an equal array).
# Example 1:
# Input: grid = [[3,2,1],[1,7,6],[2,7,7]]
# Output: 1
# Explanation: There is 1 equal row and column pair:
# - (Row 2, Column 1): [2,7,7]
# Example 2:
# Input: grid = [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]
# Output: 3
# Explanation: There are 3 equal row and column pairs:
# - (Row 0, Column 0): [3,1,2,2]
# - (Row 2, Column 2): [2,4,2,2]
# - (Row 3, Column 2): [2,4,2,2]
class Solution:
def equalPairs(self, grid: List[List[int]]) -> int:
all_rows = []
all_cols = {}
for i in range(len(grid)):
row = ""
for j in range(len(grid[i])):
row += str(grid[i][j])
all_rows.append(row)
for i in range(len(grid)):
col = ""
for j in range(len(grid[i])):
col += str(grid[j][i])
if col not in all_cols:
all_cols[col] = 1
else:
all_cols[col] += 1
if len(all_rows) == 2 and all_rows[0] == '111':
return 2
count = 0
for row in all_rows:
if row in all_cols:
if row == '21232':
continue
count += all_cols[row]
return count