Group Anagrams
Riverbend Puzzle Weekly keeps a pile of candidate answers for its crossword grids. Two words are anagrams when each can be spelled using exactly the letters of the other — same letters, same number of each, only the order differs, as with eat and tea. A setter may never use two anagrams in one grid, so the pile is filed into sets of words built from identical letters, and the filing order is fixed so that two editors reading it build the same grid.
Task: Print the words filed into sets, one set per line, so that anagrams of one another share a line.
Input
A single line holding between 1 and 1000 lowercase words separated by single spaces. Each word is at most 20 letters.
Output
One line per set, words separated by single spaces. Inside a line, words keep their input order. The lines are ordered by where each set's first word appeared.
Example:
Input:
eat tea tan ate nat bat
Output:
eat tea ate
tan nat
bat
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in