> For the complete documentation index, see [llms.txt](https://monica-granbois.gitbook.io/cs-theory-and-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://monica-granbois.gitbook.io/cs-theory-and-problems/problems/leetcode/49.-group-anagrams.md).

# 49. Group Anagrams

## Description

See: <https://leetcode.com/problems/group-anagrams/>

## Solution

When dealing with anagrams, sort the word. Anagram strings will be the same after sorting.&#x20;

```python
class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        anagrams = defaultdict(list)
        for word in strs:
            key = "".join(sorted(list(word)))
            anagrams[key].append(word)
        
        return anagrams.values()
```
