Skip to content

Commit 9b0e629

Browse files
committed
feat: [LeetCode #2352] Equal Row And Column Pairs
Тип: HashMap Сложность: Medium Временная сложность: O(n^2) Пространственная сложность: O(n) - Ссылка: https://leetcode.com/problems/equal-row-and-column-pairs/
1 parent c17b817 commit 9b0e629

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/hashmap_set/equal_row_and_column_pairs/__init__.py

Whitespace-only changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List
2+
from collections import defaultdict
3+
4+
5+
class Solution:
6+
def equalPairs(self, grid: List[List[int]]) -> int:
7+
count = 0
8+
hash = defaultdict(int)
9+
10+
for row in grid:
11+
hash[str(row)] += 1
12+
13+
for col_index in range(len(grid[0])):
14+
col = []
15+
for row_index in range(len(grid)):
16+
col.append(grid[row_index][col_index])
17+
count += hash[str(col)]
18+
19+
return count
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pytest
2+
from src.hashmap_set.equal_row_and_column_pairs.solution import (
3+
Solution,
4+
)
5+
6+
7+
@pytest.mark.parametrize(
8+
"grid, expected",
9+
[
10+
([[3, 2, 1], [1, 7, 6], [2, 7, 7]], 1),
11+
([[3, 1, 2, 2], [1, 4, 4, 5], [2, 4, 2, 2], [2, 4, 2, 2]], 3),
12+
],
13+
)
14+
def test_equal_pairs(grid, expected):
15+
solution = Solution()
16+
assert solution.equalPairs(grid) == expected

0 commit comments

Comments
 (0)