[NeetCode] Anagram Groups
NeetCode synced attempts for Anagram Groups.
Tags: algorithms, neetcode, python
Categories: NeetCode
- Problem
- Synced automatically from
devbattery/neetcode-submissions
Notes
Write your own notes here. This section is preserved across syncs.
Attempts
Attempt 1 · 2026-04-22 · Python
- Commit:
d98d180 - Source:
Data Structures & Algorithms/anagram-groups/submission-0.py
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
answer = defaultdict(list)
for s in strs:
answer["".join(sorted(s))].append(s)
print(answer)
return list(answer.values())
Attempt 2 · 2026-04-22 · Python
- Commit:
b1d69f0 - Source:
Data Structures & Algorithms/anagram-groups/submission-2.py
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
answer = defaultdict(list)
for s in strs:
cnt = [0] * 26 # a to z
for c in s:
cnt[ord(c) - ord('a')] += 1 # a: 0, b: 1, ...
answer[tuple(cnt)].append(s) # key of defaultdict(list) can't be list
return list(answer.values())
Leave a comment