Given an array of strings, group the anagrams together. An anagram is a word formed by rearranging the letters of another word, using all original letters exactly once.
For example, given ["eat", "tea", "tan", "ate", "nat", "bat"], the output should be [["bat"], ["nat", "tan"], ["ate", "eat", "tea"]].
Explain your approach, describe how you identify anagrams efficiently, and analyze the time and space complexity.
How to approach it
- Hint 1
Two words are anagrams if they have the same letters in the same quantities. How can you create a canonical form?
- Hint 2
Sorting each word alphabetically gives anagrams the same key -- 'eat' and 'tea' both become 'aet'.
- Hint 3
Alternatively, count the frequency of each letter. Two words with identical frequency counts are anagrams.
Ready to answer it out loud?
Record your answer in 1:30 and Preptile scores it 1–10 with specifics — what landed, what you skipped, and what to say next time.
Practising needs an invite code. Join the waitlist and we’ll send you one.