Skip to content

Commit 1e5b507

Browse files
authored
feat: [LeetCode #1657] Determine If Two Strings Are Close (#22)
Тип: Hashmap Сложность: medium Временная сложность: O(n log n + m log m) Пространственная сложность: O(n + m) - Ссылка: https://leetcode.com/problems/determine-if-two-strings-are-close/
1 parent e1e980a commit 1e5b507

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/hashmap_set/determine_if_two_strings_are_close/__init__.py

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from collections import Counter
2+
3+
4+
class Solution:
5+
def closeStrings(self, word1: str, word2: str) -> bool:
6+
word1_counter = Counter(word1)
7+
word2_counter = Counter(word2)
8+
9+
return (
10+
sorted(word1_counter.values()) == sorted(word2_counter.values())
11+
and word2_counter.keys() == word2_counter.keys()
12+
)
13+
14+
15+
print(Solution().closeStrings(word1="abc", word2="bca"))
16+
print(Solution().closeStrings(word1="a", word2="aa"))
17+
print(Solution().closeStrings(word1="cabbba", word2="abbccc"))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest
2+
from src.hashmap_set.determine_if_two_strings_are_close.solution import (
3+
Solution,
4+
)
5+
6+
7+
@pytest.mark.parametrize(
8+
"word1, word2, expected",
9+
[
10+
("abc", "bca", True),
11+
("a", "aa", False),
12+
("cabbba", "abbccc", True),
13+
],
14+
)
15+
def test_close_strings(word1, word2, expected):
16+
solution = Solution()
17+
assert solution.closeStrings(word1, word2) == expected

0 commit comments

Comments
 (0)