Skip to content

Commit ff1a25a

Browse files
committed
Add solution for "Group Anagrams" and update README
1 parent 7ca2c0b commit ff1a25a

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
|0217|[Contains Duplicate](https://leetcode.com/problems/contains-duplicate/description/) | [Python](./array_hashing/0217-contains-duplicate.py)|Array & Hashing| Easy |
88
|0242|[Valid Anagram](https://leetcode.com/problems/valid-anagram/description/) | [Python](./array_hashing/0242-valid-anagram.py)|Array & Hashing| Easy |
99
|0001|[Two Sum](https://leetcode.com/problems/two-sum/description/) | [Python](./array_hashing/0001-two-sum.py)|Array & Hashing| Easy |
10+
|0049|[Group Anagrams](https://leetcode.com/problems/group-anagrams/description/) | [Python](./array_hashing/0049-group-anagrams.py)|Array & Hashing| Medium |
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution(object):
2+
def groupAnagrams(self, strs):
3+
"""
4+
:type strs: List[str]
5+
:rtype: List[List[str]]
6+
"""
7+
anagrams_dict = defaultdict(list)
8+
9+
for s in strs: # n
10+
count = [0] * 26 # a ... z
11+
12+
for c in s: # m
13+
# Map a - 0, b - 1, ..., z - 25
14+
count[ord(c) - ord("a")] += 1
15+
# immutable -> hashable
16+
key = tuple(count)
17+
anagrams_dict[key].append(s)
18+
19+
# print(anagrams_dict)
20+
21+
return anagrams_dict.values()
22+
23+
# Time: O(n * m) Space: O(n * m)
24+
25+

array_hashing/0125-valid-palindrome.py

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution(object):
2+
def topKFrequent(self, nums, k):
3+
"""
4+
:type nums: List[int]
5+
:type k: int
6+
:rtype: List[int]
7+
"""
8+

0 commit comments

Comments
 (0)