Skip to main content

Group Anagrams

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

Problems16 / 152
Sign in

Group Anagrams

MEDIUMStringHash Table

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

Example 1

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

Example 2

Input:  strs = [""]
Output: [[""]]
Show hint

Use a hash map where key is sorted string (or character count) and value is list of anagrams. Group strings with same key.

Draft saves automatically

Loading editor...

Submit also runs hidden tests
strs =
["eat","tea","tan","ate","nat","bat"]
Expected
[["eat","tea","ate"],["tan","nat"],["bat"]]
Run with your own input (JSON)