From 1836e5f8215f612b783544e131eed8e55930ec60 Mon Sep 17 00:00:00 2001 From: BHARAT0310 <55924751+BHARAT0310@users.noreply.github.com> Date: Tue, 12 Apr 2022 15:07:32 +0530 Subject: [PATCH] Update 49-Group-Anagrams.py Run time and Memory usage of the above solution is. R.T = 140ms M.U = 18.4MB --- 49-Group-Anagrams.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/49-Group-Anagrams.py b/49-Group-Anagrams.py index a52b995a2..fe78b51f1 100644 --- a/49-Group-Anagrams.py +++ b/49-Group-Anagrams.py @@ -1,10 +1,7 @@ class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: - ans = collections.defaultdict(list) - + hashmap = defaultdict(list) for s in strs: - count = [0] * 26 - for c in s: - count[ord(c) - ord('a')] += 1 - ans[tuple(count)].append(s) - return ans.values() + # keys can be strings, bcz they are immutable. + hashmap[str(sorted(s))].append(s) + return hashmap.values()
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: