29 · Topological Sort

#953 Verifying an Alien Dictionary

Easysolve on LeetCode ↗

frequently reported atMetaAmazon

the problem

In plain words

An alien language uses the 26 lowercase letters in a different order, given as order. Return true if words are sorted lexicographically in that alien order.

in words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"

out true

'h' comes before 'l' in this language.

in words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"

out false

'd' comes after 'l', so "word" > "world".

in words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"

out false

A word must come after its own prefix.

  • 1 ≤ words.length ≤ 100
  • 1 ≤ words[i].length ≤ 20
  • order.length == 26

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #269
      Alien Dictionary
      Hardin Topo SortLeetCode Premium
    2. #14
      Longest Common Prefix
      Easyin Arrays & Strings