Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2782b42

Browse files
committed
added: group anagrams
1 parent 93a29b3 commit 2782b42

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# group anagrams | leetcode 49 | https://leetcode.com/problems/group-anagrams/
2+
# method: dictionary with char counter as key
3+
4+
from collections import defaultdict
5+
6+
class Solution:
7+
def groupAnagrams(self, strs):
8+
grouped = defaultdict(list)
9+
10+
for each_word in strs:
11+
count_of_ch = [0] * 26
12+
for each_ch in each_word:
13+
count_of_ch[ord(each_ch) - ord("a")] += 1
14+
grouped[tuple(count_of_ch)].append(each_word)
15+
16+
return grouped.values()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /