#K70217. Word Chain Rearrangement

    ID: 33260 Type: Default 1000ms 256MiB

Word Chain Rearrangement

Word Chain Rearrangement

You are given a sequence of words. Your task is to determine whether it is possible to rearrange the words so that the last character of each word (except the last word) is the same as the first character of the next word.

Formally, given a list of words \(w_1, w_2, \dots, w_n\), you need to check if there exists a permutation \(p\) of \(\{1, 2, \dots, n\}\) such that for every \(1 \leq i < n\), the last letter of \(w_{p(i)}\) is equal to the first letter of \(w_{p(i+1)}\).

For example, for the sequence ["apple", "egg", "giraffe"], one possible rearrangement is the same order, because the last character of "apple" ('e') matches the first character of "egg" ('e'), and the last character of "egg" ('g') matches the first character of "giraffe" ('g').

inputFormat

The input is read from standard input (stdin). The first line contains a positive integer (n) representing the number of words. The second line contains (n) words separated by spaces.

outputFormat

Output to standard output (stdout) a single line containing either "yes" if it is possible to rearrange the words to form a valid chain, or "no" otherwise.## sample

3
apple egg giraffe
yes

</p>