#K11916. Anagram Groups

    ID: 23575 Type: Default 1000ms 256MiB

Anagram Groups

Anagram Groups

Given a list of strings, your task is to identify and return all words that have at least one anagram in the list. Two words are considered anagrams if one can be rearranged to form the other using all the original letters exactly once.

For example, if the input list is "listen silent enlist hello vile evil", then "listen", "silent", "enlist" form one anagram group and "vile", "evil" form another group. Words which do not have at least one other anagram in the list should not be output.

Note: If there are multiple groups of anagrams, the order for each group is the order of their first occurrence in the input. The final output should be a single line of words (if any) separated by a space.

In mathematical terms, let \( S = [w_1, w_2, \dots, w_n] \) be the list of words. For each word \( w_i \), define its signature as \( \sigma(w_i) = \text{sort}(w_i) \). Then, the word \( w_i \) is printed if and only if there exists some \( w_j \) (with \( i \neq j \)) such that \( \sigma(w_i) = \sigma(w_j) \).

inputFormat

The first line contains an integer \( n \) (\( n \ge 0 \)) representing the number of words. The second line contains \( n \) words separated by spaces.

outputFormat

Output a single line containing the words that have an anagram partner in the list, separated by a single space. If no such words exist, output an empty line.

## sample
6
listen silent enlist hello vile evil
listen silent enlist vile evil