diff --git a/src/hashmap_set/equal_row_and_column_pairs/__init__.py b/src/hashmap_set/equal_row_and_column_pairs/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/hashmap_set/equal_row_and_column_pairs/solution.py b/src/hashmap_set/equal_row_and_column_pairs/solution.py new file mode 100644 index 0000000..c140a6b --- /dev/null +++ b/src/hashmap_set/equal_row_and_column_pairs/solution.py @@ -0,0 +1,19 @@ +from typing import List +from collections import defaultdict + + +class Solution: + def equalPairs(self, grid: List[List[int]]) -> int: + count = 0 + hash = defaultdict(int) + + for row in grid: + hash[str(row)] += 1 + + for col_index in range(len(grid[0])): + col = [] + for row_index in range(len(grid)): + col.append(grid[row_index][col_index]) + count += hash[str(col)] + + return count diff --git a/tests/test_equal_row_and_column_pairs.py b/tests/test_equal_row_and_column_pairs.py new file mode 100644 index 0000000..9c68822 --- /dev/null +++ b/tests/test_equal_row_and_column_pairs.py @@ -0,0 +1,16 @@ +import pytest +from src.hashmap_set.equal_row_and_column_pairs.solution import ( + Solution, +) + + +@pytest.mark.parametrize( + "grid, expected", + [ + ([[3, 2, 1], [1, 7, 6], [2, 7, 7]], 1), + ([[3, 1, 2, 2], [1, 4, 4, 5], [2, 4, 2, 2], [2, 4, 2, 2]], 3), + ], +) +def test_equal_pairs(grid, expected): + solution = Solution() + assert solution.equalPairs(grid) == expected