From 9b0e6292f6424bbed3dd16cfc58dc209947dce7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=92=D0=B0=D1=81?= =?UTF-8?q?=D0=B8=D0=BB=D1=8C=D0=B5=D0=B2?= Date: Sat, 15 Nov 2025 14:06:05 +0300 Subject: [PATCH] feat: [LeetCode #2352] Equal Row And Column Pairs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Тип: HashMap Сложность: Medium Временная сложность: O(n^2) Пространственная сложность: O(n) - Ссылка: https://leetcode.com/problems/equal-row-and-column-pairs/ --- .../equal_row_and_column_pairs/__init__.py | 0 .../equal_row_and_column_pairs/solution.py | 19 +++++++++++++++++++ tests/test_equal_row_and_column_pairs.py | 16 ++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 src/hashmap_set/equal_row_and_column_pairs/__init__.py create mode 100644 src/hashmap_set/equal_row_and_column_pairs/solution.py create mode 100644 tests/test_equal_row_and_column_pairs.py 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