Problem 58
Group Anagrams Count
Medium · 20 pts · String, Hash Table, Sorting
Problem
You are given N words. Two words are anagrams of each other if they contain the same letters with the same counts, possibly in a different order. Put the words into groups so that each group contains words that are all anagrams of each other, and print the number of groups. Anagrams have the same letters in sorted order, so the sorted word works as a key: "eat", "tea" and "ate" all become "aet". The answer is the number of distinct keys.
Input
First line: integer N. Second line: N words of lowercase letters separated by spaces.
Output
Print the number of anagram groups.
Constraints
1 <= N <= 10000; 1 <= word length <= 100
Example input
6 eat tea tan ate nat bat
Example output
3
Explanation
Sorting the letters of each word gives aet for eat, tea and ate, ant for tan and nat, and abt for bat. There are 3 distinct keys, so there are 3 groups.