We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d1c71ec commit f4f5256Copy full SHA for f4f5256
49-Group-Anagrams.py
@@ -1,7 +1,10 @@
1
class Solution:
2
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
3
- hashmap = defaultdict(list)
+ ans = collections.defaultdict(list)
4
+
5
for s in strs:
- # keys can be strings, bcz they are immutable.
6
- hashmap[str(sorted(s))].append(s)
7
- return hashmap.values()
+ count = [0] * 26
+ for c in s:
8
+ count[ord(c) - ord('a')] += 1
9
+ ans[tuple(count)].append(s)
10
+ return ans.values()
0 commit comments