DIY: Verifying an Alien Dictionary
Solve the interview question "Verifying an Alien Dictionary" in this lesson.
We'll cover the following
Problem statement
In this coding exercise, you are given a list of words written in an alien language. Surprisingly, the aliens also use lowercase English letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters of the English language.
Given a vector of words written in the alien language and the order of the alphabet, return True
if and only if the given words are sorted lexicographically in this alien language.
Constraints
You can assume the following constraints:
- 1 <=
words.length
<= 100 - 1 <=
words[i].length
<= 20 order.length
== 26- All the characters in
words[i]
andorder
are lowercase English letters.
Input
The input will contain a vector of strings, words
, and a string, order
. Here are two example inputs to the function:
# Sample Input 1
words = ["hello", "world"]
order = "hwabcdefgijklmnopqrstuvxyz"
# Sample Input 2
words = ["educated", "educate"]
order = "educatebfghijklmnopqrsvwxyz"
Output
For the input given above, the output will be:
# Sample Output 1
True
# Sample Output 2
False
Coding exercise
For this coding exercise, you have to implement the verifyAlienDictionary(words, order)
function, wherein words
represents a vector of words and the order
represents the order of the alphabets. The function will return True
or False
depending on whether the given vector of words belongs to the alien language or not.
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.