DIY: Verifying an Alien Dictionary

Solve the interview question "Verifying an Alien Dictionary" in this lesson.

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 an array 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] and order are lowercase English letters.

Input

The input will contain an array 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 an array of words and the order represents the order of the alphabets. The function will return true or false depending on whether the given array of words belongs to the alien language or not.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.