• 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

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

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