Problem · Medium · 25 min
Group the anagrams
- Medium
- Hash maps
- Strings
- Sorting
Two words are anagrams when they use the same letters the same number of times. For example, "listen" and "silent" are anagrams.
Write groupAnagrams(words). It takes an array of words and returns an array of groups. Each group is an array of words that are anagrams of each other.
- The words use lower case letters only. No word is empty.
- Order the groups by the first word of each group in the input.
- Inside a group, keep the order of the input.
- A word with no anagram makes a group of its own.
- The same word can appear more than once. Keep every copy.
Examples
| Call | Result |
|---|---|
groupAnagrams(['listen', 'silent', 'enlist', 'google', 'gogole', 'cat']) |
[['listen', 'silent', 'enlist'], ['google', 'gogole'], ['cat']] |
groupAnagrams(['abc', 'bca', 'xyz']) |
[['abc', 'bca'], ['xyz']] |
groupAnagrams([]) |
[] |
Tasks
2 to do
Task 1, not checked yet. Make
groupAnagramspass the example tests.Task 2, not checked yet. Make
groupAnagramspass the hidden tests.
Task 1 of 2
Make groupAnagrams pass the example tests.