Group Anagrams
medium⏱ 15 mintypescriptchallengesproblem-solving-intermediate-
6
eat tea tan ate nat bat
Sample case — your program reads stdin and prints to stdout.
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