Skip to content

Commit 421eb68

Browse files
committed
Group Anagrams (#166)
* Group Anagrams * Fix new line
1 parent 6f25bd4 commit 421eb68

4 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎binary_tree/tree_node.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
class TreeNode(Generic[T]):
7-
def __init__(self, val: T):
7+
def __init__(self, val: T, left: Optional['TreeNode'] = None, right: Optional['TreeNode'] = None):
88
self.val = val
9-
self.left: Optional[TreeNode] = None
10-
self.right: Optional[TreeNode] = None
9+
self.left: Optional[TreeNode] = left
10+
self.right: Optional[TreeNode] = right

‎strings/README.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* [Add Binary](#add-binary)
44
* [Crop String](#crop-string)
55
* [First Unique Character](#first-unique-character-in-a-string)
6+
* [Group Anagrams](#group-anagrams)
7+
* [Implement strStr](#implement-strstr)
68
* [Jewels and Stones](#jewels-and-stones)
79
* [License Key Formatter](#license-key-formatter)
810
* [Longest Common Prefix](#longest-common-prefix)
@@ -122,6 +124,26 @@ def first_unique_character_in_str(val: str) -> int:
122124
return -1
123125
```
124126

127+
## Group Anagrams
128+
129+
Given an array of strings, group the anagrams together.
130+
131+
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the
132+
original letters exactly once.
133+
134+
```python
135+
def group_anagrams(strings: List[str]) -> List[List[str]]:
136+
sorted_strings = ["".join(sorted(string)) for string in strings]
137+
answer = {}
138+
139+
for index, value in enumerate(strings):
140+
if sorted_strings[index] in answer:
141+
answer[sorted_strings[index]].append(value)
142+
else:
143+
answer[sorted_strings[index]] = [value]
144+
return list(answer.values())
145+
```
146+
125147
## Implement strStr
126148

127149
Implement `strStr()`.

‎strings/group_anagrams.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from typing import List
2+
3+
4+
def group_anagrams(strings: List[str]) -> List[List[str]]:
5+
sorted_strings = ["".join(sorted(string)) for string in strings]
6+
answer = {}
7+
8+
for index, value in enumerate(strings):
9+
if sorted_strings[index] in answer:
10+
answer[sorted_strings[index]].append(value)
11+
else:
12+
answer[sorted_strings[index]] = [value]
13+
return list(answer.values())
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from strings.group_anagrams import group_anagrams
2+
3+
4+
def test_group_anagrams():
5+
strings = []
6+
assert group_anagrams(strings) == []
7+
8+
strings = ['test']
9+
assert group_anagrams(strings) == [['test']]
10+
11+
strings = ['eat', 'tea', 'tan', 'ate', 'nat', 'bat']
12+
assert group_anagrams(strings) == [['eat', 'tea', 'ate'], ['tan', 'nat'], ['bat']]

0 commit comments

Comments
 (0)