Imagine you're working as a linguist studying an alien civilization that has just made contact with Earth. Surprisingly, these aliens use the same English lowercase letters as we do, but they've arranged their alphabet in a completely different order!
You've been given two crucial pieces of information:
- A
sequence of wordswritten in this alien language - The
order of their alphabet(a permutation of our 26 lowercase letters)
Your mission is to determine whether the given words are sorted lexicographically according to the alien alphabet order. In other words, would these words appear in the correct order in an alien dictionary?
For example: If the alien alphabet order is "hlabcdefgijkmnopqrstuvwxyz" (where 'h' comes before 'a'), then the word "hello" would come before "apple" in their dictionary, which is opposite to our English ordering!
Return true if the words are properly sorted according to the alien alphabet, false otherwise.
Input & Output
Visualization
Time & Space Complexity
N is number of words, M is average word length. Hash map lookups are O(1)
Hash map stores at most 26 character positions (constant space)
Constraints
- 1 โค words.length โค 100
- 1 โค words[i].length โค 20
- order.length == 26
- All characters in words[i] and order are English lowercase letters
- order is a permutation of the English lowercase letters