diff --git a/49-Group-Anagrams.py b/49-Group-Anagrams.py index fe78b51f1..a52b995a2 100644 --- a/49-Group-Anagrams.py +++ b/49-Group-Anagrams.py @@ -1,7 +1,10 @@ class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: - hashmap = defaultdict(list) + ans = collections.defaultdict(list) + for s in strs: - # keys can be strings, bcz they are immutable. - hashmap[str(sorted(s))].append(s) - return hashmap.values() + count = [0] * 26 + for c in s: + count[ord(c) - ord('a')] += 1 + ans[tuple(count)].append(s) + return ans.values() pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy