Equal row and column pairs
September 3, 2022
array-and-hashmapProblem URL: Equal row and column pairs
We will create a hashmap where we will count the rows occurance, the key will be the row as tuple and the value will be the count. Then we move through the column, search in the lookup hashmap for any match, if we found one, count this as out result. Finally we will return the count.
class Solution:
def equalPairs(self, grid: List[List[int]]) -> int:
ROWS, COLS = len(grid), len(grid[0])
lookup = collections.defaultdict(int)
for row in grid:
lookup[tuple(row)] += 1
res = 0
for j in range(COLS):
col = [grid[i][j] for i in range(ROWS)]
res += lookup[tuple(col)]
return res
Time Complexity: O(n*m)
, n is the number of rows and m is the number of columns
Space Complexity: O(n*m)