Verifying an Alien Dictionary

An alien language uses the same lowercase English letters, but in a different order. Given a list of words written in that language and the string order (a permutation of the 26 letters giving the alien alphabet), return true if and only if the words are sorted lexicographically in the alien language. As in an ordinary dictionary, a word that is a prefix of another comes first, so "app" must come before "apple".

30:00

Verifying an Alien Dictionary
easy
Free written solution
Topics
Companies

An alien language uses the same lowercase English letters, but in a different order. Given a list of words written in that language and the string order (a permutation of the 26 letters giving the alien alphabet), return true if and only if the words are sorted lexicographically in the alien language. As in an ordinary dictionary, a word that is a prefix of another comes first, so "app" must come before "apple".

Example 1:
Input: {"words":["hello","leetcode"],"order":"hlabcdefgijkmnopqrstuvwxyz"}
Output: true
Constraints:
  • 1≤words.length≤1001 \leq \text{words.length} \leq 100

  • 1≤words[i].length≤201 \leq \text{words}[i].\text{length} \leq 20

  • order.length==26\text{order.length} == 26

  • All characters in words[i] and order are lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"words":["hello","leetcode"],"order":"hlabcdefgijkmnopqrstuvwxyz"}

Write each letter's position in the alien alphabet: 'h' gets rank 0, 'l' rank 1, and so on.

alien alphabet (rank = position in order)

h

0

l

1

a

2

b

3

c

4

d

5

e

6

f

7

g

8

i

9

j

10

k

11

m

12

n

13

o

14

p

15

q

16

r

17

s

18

t

19

u

20

v

21

w

22

x

23

y

24

z

25

words, checked pair by pair

0

hello

·

1

leetcode

Variables
VariableValue
ranked9
words2
DepthFunction Call
Stack empty
0/6