#C9670. Circular String Arrangement
Circular String Arrangement
Circular String Arrangement
You are given a list of strings. Your task is to determine if it is possible to arrange them in a circular sequence such that the last character of each string matches the first character of the next string. In addition, the last character of the last string must match the first character of the first string to complete the cycle.
Formally, given strings \(S_1, S_2, \dots, S_n\), check if there exists a permutation such that for every \(i=1,2,\dots,n\), the last character of \(S_i\) equals the first character of \(S_{i+1}\) (with \(S_{n+1}=S_1\)).
If the list contains only one string, output YES
by default.
inputFormat
The first line contains an integer n
, the number of strings. The following n
lines each contain a non-empty string composed of lowercase English letters.
outputFormat
Output a single line containing either YES
if the strings can be arranged to form a circle as described, or NO
otherwise.
3
abc
cde
efg
NO
</p>