Verifying an Alien Dictionary

EasyStringshashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

An alien language uses the 26 lowercase English letters, but in a different alphabetical order given by the string order (a permutation of a–z, first letter smallest). Given a list words, return true if the words are sorted in non-decreasing lexicographic order under the alien alphabet, and false otherwise. Lexicographic comparison works as usual: compare at the first position where two words differ; if one word is a prefix of the other, the shorter word comes first.

Examples

Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
Output: true
`h` comes before `l` in the alien order.
Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
Output: false
At index 3, `d` ranks after `l`, so "word" should follow "world".
Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
Output: false
"app" is a prefix of "apple" and must come first.

Constraints

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 20
  • order.length == 26
  • `order` is a permutation of the lowercase English letters
  • `words[i]` consists of lowercase English letters

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems