Group anagrams
Standard · Write code · about 12 min
Given an array of strings words, group the words that are anagrams of each other. Two words are anagrams when they use the same letters the same number of times.
Return the groups in any order, with the words inside each group in any order.
Example 1
Input:
[["eat","tea","tan","ate","nat","bat"]]
Output:
[["eat","tea","ate"],["tan","nat"],["bat"]]
eat, tea and ate all sort to aet.
Example 2
Input:
[["a"]]
Output:
[["a"]]
Constraints
- 0 ≤ words.length ≤ 10,000
- 0 ≤ words[i].length ≤ 20
- Words use lowercase English letters
Learn the idea first: Hash sets and maps in practice: counting, lookup, deduplication, a visual lesson.
More Hash sets and maps in practice: counting, lookup, deduplication problems
- Contains duplicate Warm-up
- Build a frequency map Warm-up
- Two Sum Standard
- First unique character Standard
- Intersection of arrays Standard
- Longest consecutive sequence Challenge