49. Group Anagrams
All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : June 13, 2024
Last updated : July 01, 2024
Related Topics : Array, Hash Table, String, Sorting
Acceptance Rate : 69.41 %
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
output = {}
for s in strs :
key = ''.join(sorted(s))
if key in output :
output[key].append(s)
else :
output[key] = [s]
return [x for x in output.values()]
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
output = {}
for s in strs :
cnt = [0] * 26
for c in s :
cnt[ord(c) - ord('a')] += 1
cnt = tuple(cnt)
if cnt in output :
output[cnt].append(s)
else :
output[cnt] = [s]
return [x for x in output.values()]