Similar Words
InterviewTime limit2sMemory limit128 MB
Count unordered pairs of equal-length words that are related by some bijection between letters, similar to the isomorphic-strings check.
- Level
Medium4 of 10
- Topics
- String, Hash map, Combinatorics
- Solved
- No attempts yet
Problem
Two words A and B are similar if it is possible to assign one lowercase letter to each distinct letter in A so that replacing every letter of A by its assigned letter produces B.
Equal letters in A must therefore become equal letters in B, and two different letters in A may not become the same letter in B. A letter may be assigned to itself.
For instance, assign a to z, b to b, and c to x in abca. The result is zbxz, so abca and zbxz are similar.
Given several words, count how many unordered pairs of words are similar.
Input
The first line contains the number of words N. Each of the next N lines contains one word.
N is a positive integer not greater than 100. Each word has length at most 50. All words have the same length, no two words are identical, and every word consists only of lowercase English letters.
Output
Print the number of unordered pairs of similar words.