Group words that are anagrams of each other (the same letters in a different order).
Input
The first line has n (1 ≤ n ≤ 10⁴). The second line has n lower-case words.
Output
One group per line, its words separated by spaces in the order they appear in the input. The groups are in the order of their first word's appearance.
Examples
Example 1 · Input
6 eat tea tan ate nat bat
Expected output
eat tea ate tan nat bat
Example 2 · Input
1 a
Expected output
a
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Two words are anagrams when their sorted letters are equal.
Hint 2
Use the sorted letters as a dictionary key; the value is the list of words.
Hint 3
Python dictionaries keep insertion order, so groups come out in order of first appearance.
Solution
Show one correct solution (try it yourself first)
n = int(input())
groups = {}
for w in input().split():
groups.setdefault("".join(sorted(w)), []).append(w)
for g in groups.values():
print(" ".join(g))