#K92407. Finding the Key Word in Anagrams
Finding the Key Word in Anagrams
Finding the Key Word in Anagrams
You are given several test cases. In each test case, you are given a list of words. A key word is defined as a word that has at least one other word in the list that is its anagram (i.e. the letters of one word can be rearranged to form the other word). If there are multiple valid key words, choose the one with the longest length. In case of a tie, choose the one that appears first in the input order. If no word has an anagram match in the list, output No key word
.
Note: Two words are anagrams if their sorted sequences of characters are identical.
The relation can be expressed in LaTeX as follows:
$$ word_i\ is\ a\ key\ word \iff \exists\ j\ (i \neq j \wedge sort(word_i)=sort(word_j)) $$inputFormat
The input is read from standard input (stdin) and has the following format:
t W1 word1,1 word1,2 ... word1,W1 W2 word2,1 ... word2,W2 ...
Where:
t
is the number of test cases.- For each test case, the first integer
W
indicates the number of words followed byW
words, each on a new line.
outputFormat
For each test case, output the key word on a separate line to standard output (stdout). If no key word exists, output No key word
.
2
5
listen
enlist
google
inlets
silent
4
abc
def
ghi
jkl
listen
No key word
</p>