#C13523. Group Isomorphic Words
Group Isomorphic Words
Group Isomorphic Words
Given a list of lowercase words, you need to group the words that are isomorphic. Two words are said to be isomorphic if there exists a one-to-one mapping, i.e. a bijection, between every character of the first word and every character of the second word while preserving the order. Formally, two words w1 and w2 are isomorphic if there exists a function \( f: \Sigma \to \Sigma \) (where \( \Sigma \) is the set of lowercase letters) such that for every index \( i \), \( f(w_{1}[i]) = w_{2}[i] \), and \( f \) is one-to-one.
Your task is to read a list of words from standard input, group the words that are isomorphic, and then print each group on a new line. Within each group, the words should be printed in alphabetical order, and the groups themselves should be sorted in lexicographical order according to their first word.
For example, the words "foo" and "egg" are isomorphic because they share the same positional character mapping, while "foo" and "bar" are not.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains an integer \( N \) representing the number of words.
- The second line contains \( N \) lowercase words separated by spaces.
outputFormat
Output to standard output (stdout) should consist of several lines. Each line represents a group of isomorphic words. Within each group, the words should be sorted in alphabetical order, and the groups should be sorted in lexicographical order by the first word of each group. If there are no words (i.e. \( N = 0 \)), then no output should be produced.
## sample0