#B4069. Non-decreasing Concatenation
Non-decreasing Concatenation
Non-decreasing Concatenation
Xiao Yang has \( n \) strings \( s_1, s_2, \ldots, s_n \), each consisting only of lowercase English letters. He wants to arrange these strings in some order and then concatenate them together to form a new string \( t \). The string \( t \) must satisfy the following condition:
For every position \( i \) (1-indexed) in \( t \) with character \( t_i \), for all positions \( j < i \) it holds that \( t_j \le t_i \) (the order of characters is defined by their order in the English alphabet, e.g. \( \mathtt{e} < \mathtt{g} < \mathtt{p} < \mathtt{s} \)).
Determine whether there exists a permutation of the given strings such that when concatenated, the resulting string \( t \) is non-decreasing (i.e. sorted in ascending order).
Note: You are not allowed to change the characters within any string. Also, if a string itself is not non-decreasing (i.e. its characters are not in sorted order), then it cannot be part of any valid concatenation.
inputFormat
The input begins with a single integer \( n \) indicating the number of strings. The following \( n \) lines each contain a non-empty string composed solely of lowercase English letters.
Input Format:
n s1 s2 ... sn
outputFormat
Print a single line containing either "Yes" if there exists a permutation of the strings such that the concatenation is non-decreasing, or "No" if no such permutation exists.
sample
2
ab
bb
Yes