#K38577. Longest Contiguous Alphabetical Word
Longest Contiguous Alphabetical Word
Longest Contiguous Alphabetical Word
Given a list of words, find the longest word in which each consecutive pair of letters is alphabetically contiguous. That is, for a word \(w = w_1w_2\dots w_n\), it is required that for every \(i\) from 1 to \(n-1\), \(\mathrm{ord}(w_{i+1}) = \mathrm{ord}(w_i) + 1\). In case there are multiple words with the same maximum length, select the one that appears first in the list. If no such word exists, output an empty string.
Example Explanation: For the list ["abc", "ade", "xyz", "abcde"], the word "abcde" is the longest where each letter follows its predecessor consecutively (i.e., a\(\to\)b, b\(\to\)c, c\(\to\)d, d\(\to\)e). You must implement the solution based on input from stdin
and output the result to stdout
.
inputFormat
The input is read from stdin
as follows:
- 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 string to stdout
— the longest contiguous alphabetical word. If no word meets the criteria, output an empty string.
4
abc ade xyz abcde
abcde