Skip to content

Instantly share code, notes, and snippets.

@ksamirdev
Created April 5, 2026 06:00
Show Gist options
  • Select an option

  • Save ksamirdev/ddc827b9dc5f3b66a5733d33e67bb40c to your computer and use it in GitHub Desktop.

Select an option

Save ksamirdev/ddc827b9dc5f3b66a5733d33e67bb40c to your computer and use it in GitHub Desktop.
from collections import defaultdict
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagrams = defaultdict(list)
for st in strs:
count = [0] * 26
for c in st:
count[ord(c) - ord('a')] += 1
key = tuple(count)
anagrams[key].append(st)
return list(anagrams.values())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment