# 49. Group Anagrams

Given an array of strings `strs`, group the anagrams together. You can return the answer in **any order**.

Example-

**Input:** strs = \["eat","tea","tan","ate","nat","bat"\]

**Output:** \[\["bat"\],\["nat","tan"\],\["ate","eat","tea"\]\]

definition- An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once.

![](https://cdn.hashnode.com/uploads/covers/66096e456083ba84715fd663/f4e66236-e087-427a-b545-c3ec6c0b54c9.png align="center")

can we find a pattern here? But on a closer look , do we need to find a pattern? Because the pattern is mentioned in the question itself- anagram. abt - bat, ant-nat,tan , aet- ate,eat,tea. So if we sort the letters alphabetically we can can make a group heading or identification check of the group, and if it doesnot exist already, we create a new group.  
sets can be used here, because uniqueness is involved. Or even hashmap.

### Implementation

```plaintext
        res = defaultdict(list)
        for s in strs:
            sortedS = ''.join(sorted(s))
            res[sortedS].append(s)
        return list(res.values())
```
