An inventory lists n labels, each made of lowercase English letters. Count the number of unordered pairs of positions (i, j) with i < j such that the two labels are anagrams of each other (one can be rearranged into the other).
Line 1: an integer n, the number of labels.
Line 2: n space-separated labels, each 1 to 20 lowercase English letters.
A single integer: the number of anagram pairs.
Example 1
Input
4 cat act tac dog
Expected
3
Explanation
cat, act and tac are mutual anagrams, giving 3 pairs among them; dog pairs with none. Total 3.
Example 2
Input
3 ab cd ef
Expected
0
Explanation
No two labels are anagrams, so there are 0 anagram pairs.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →