Turning Learners Into Developers
Codekilla
CODEKILLA
Medium
JAVA-002java

Group Anagrams

Problem

Given a list of words, group them into anagram families. Any ordering is acceptable.

Input
Line 1: n. Next n lines: one word each (lowercase).
Output
Groups on separate lines; words within a group space-separated, sorted alphabetically within the group.
Constraints
1 ≤ n ≤ 10^4, 1 ≤ |word| ≤ 100
Sample input
6
eat
tea
tan
ate
nat
bat
Sample output
ate eat tea
bat
nat tan
Explanation
Sorting each word gives its 'anagram key'. Words with the same key belong in the same group.
hash-mapstringssorting@Amazon@Facebook@Adobe
Visible test cases
in: 6
eat
tea
tan
ate
nat
bat
out: ate eat tea
bat
nat tan
Your solution — run it, use AI if stuck
java