#K88607. Alien Dictionary Order Verification
Alien Dictionary Order Verification
Alien Dictionary Order Verification
You are given a list of words and a string representing the custom order of the alien alphabet. Your task is to determine whether the words are sorted in lexicographical order according to the alien language.
The alien language uses the same set of lowercase English letters but the order of the letters is different. Formally, given a list of words and an order string, you need to verify if for every adjacent pair of words \(w_i\) and \(w_{i+1}\), the following holds:
[ \text{if } w_i = a_1a_2\ldots a_k, ; w_{i+1} = b_1b_2\ldots b_m, ; \text{then either } a_1 = b_1, a_2 = b_2, ..., a_{j-1} = b_{j-1} \text{ and } a_j < b_j \text{ (according to the custom order)} \text{ or } k \le m ]
For example, given words ["hello", "leetcode"]
and order "hlabcdefgijkmnopqrstuvwxyz"
, the list is sorted because 'h' comes before 'l' in the provided order.
inputFormat
The input is given via stdin in the following format:
n word1 word2 ... wordn order
Where:
n
is a positive integer denoting the number of words.- The second line contains
n
words separated by spaces (each word consists of lowercase English letters). order
is a string of length 26 representing the custom order of the alien alphabet.
outputFormat
Output a single line to stdout with the string True
if the list of words is sorted according to the given alien alphabet order, and False
otherwise.
2
hello leetcode
hlabcdefgijkmnopqrstuvwxyz
True