n: strs.length
k: average str.length
Time Complexity: O(nklogk)
Space Complexity: O(nk)
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
hashtable = dict()
for str in strs:
key = ''.join(sorted(list(str)))
if not key in hashtable:
hashtable[key] = list()
hashtable[key].append(str)
return hashtable.values()
I found that solution very popular and helpful:
https://www.youtube.com/watch?v=n80QtzugEP8&ab_channel=EricProgramming