You are working with an unknown language that uses a custom alphabetical order, such as ['a', 'd', 'c', 'b', ...].
Implement the following:
-
Check sorted order
: Given a custom alphabet order and an array of strings, determine whether the strings are sorted in nondecreasing lexicographic order under that alphabet.
-
Follow-up
: Given an array of strings that is already sorted according to an unknown custom alphabet, infer
one possible
alphabet order that is consistent with the given list. If no valid order exists, return that it is impossible.
Assumptions:
-
All strings consist of lowercase letters.
-
Lexicographic comparison is based on the first differing character.
-
If one word is a prefix of another, the shorter word must come first.
-
In the follow-up, if multiple valid alphabet orders exist, returning any one of them is acceptable.