Group Anagrams
medium⏱ 15 mintypescriptchallengesproblem-solving-intermediate-
LEARN BY DOINGPut your intuition to work3 steps
Group Anagrams
Use the lesson’s examples and explanation to check your idea.
Personal reflection. Nothing is sent to an AI service.6
eat tea tan ate nat bat
ate eat tea
bat
nat tan
Sample case — your program reads stdin and prints to stdout.
Starter Given an array of strings, group the anagrams together. Anagrams are words with the same characters in different order.
Algorithm: Sort each word's characters and use as key in a hash map.
Example: ['eat','tea','tan','ate','nat','bat'] → Groups: ['eat','tea','ate'], ['tan','nat'], ['bat']
Input format
Line 1: n (number of words). Line 2: n space-separated words.
Output format
Each group on a line, words space-separated, groups sorted alphabetically by first word, words within groups sorted.
Constraints
1 ≤ n ≤ 10⁴, 0 ≤ word.length ≤ 100, words are lowercase letters