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.
2 parents 4189a2c + 1836e5f commit d81cdf1Copy full SHA for d81cdf1
49-Group-Anagrams.py
@@ -1,10 +1,7 @@
1
class Solution:
2
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
3
- ans = collections.defaultdict(list)
4
-
+ hashmap = defaultdict(list)
5
for s in strs:
6
- count = [0] * 26
7
- for c in s:
8
- count[ord(c) - ord('a')] += 1
9
- ans[tuple(count)].append(s)
10
- return ans.values()
+ # keys can be strings, bcz they are immutable.
+ hashmap[str(sorted(s))].append(s)
+ return hashmap.values()
0 commit comments