mediumUnit 7 · Dictionaries & Sets
P21. Anagram Groups
Dictionaries & Sets
Two words are anagrams if they contain the same letters in a different order (for example listen and silent). Read a list of lowercase words and group the anagrams together using a dictionary whose key is the word's letters in sorted order.
Print each group on its own line, with the words separated by spaces. Groups appear in the order their first word appears, and words inside a group keep their input order. A repeated word is included only once.
Input Format
- Line 1: lowercase words separated by spaces
Output Format
- One line per group
- Last line:
Groups: <count>
Constraints
- 1 ≤ number of words ≤ 100
Sample Test Cases
Sample 1
Input
listen silent enlist google elgoog cat act
Output
listen silent enlist google elgoog cat act Groups: 3
Sample 2
Input
python
Output
python Groups: 1
Submitting checks 2 sample cases and 3 hidden test cases.