◀ THE GRIND — GRAPHS

Verifying An Alien Dictionary

The drill: Words are supposed to be sorted according to some alien alphabet — not a,b,c but a custom 26-letter order. Confirm every adjacent pair in the list respects that ordering, the way ordinary dictionary order works in English.

THE BRIEFING — THE FULL DRILL, IN MY OWN WORDS

A list of words arrives alongside a string that defines a full replacement alphabet — the same 26 letters, just reshuffled into a new order. The task is to confirm the word list is already sorted under that reshuffled order.

Comparison works exactly like ordinary dictionary sorting, just with a different letter ranking: walk two neighboring words character by character, and the first pair of differing letters decides which word comes first under the alien ranking.

When one word runs out of letters before any difference appears, it must be the shorter word and it has to come first — a word that is merely a prefix of the next one is never allowed to sort after it. One violation anywhere in the list fails the whole check.

EX 01
words = ["banana", "ban"] · order = "abcdefghijklmnopqrstuvwxyz"
false
PREFIX SHOULD COME FIRST BUT DOESN'T
EX 02
words = ["ban", "banana"] · order = "abcdefghijklmnopqrstuvwxyz"
true
PREFIX CORRECTLY COMES FIRST
EX 03
words = ["z", "a"] · order = "zyxwvutsrqponmlkjihgfedcba"
true
REVERSED ALIEN ALPHABET, Z SORTS BEFORE A
THE HINTS — TAKE ONLY WHAT YOU NEED
HINT 1 THE NUDGE

This is exactly dictionary comparison — the only twist is that the alphabet's order isn't a-through-z. What single piece of information would let you compare two letters instantly?

HINT 2 THE STRUCTURE

Build a rank for every letter of the alien alphabet — its position in the order string. Comparing two words is now comparing sequences of ranks instead of letters.

HINT 3 ONE STEP FROM THE ANSWER

Compare each adjacent pair of words rank by rank: the first differing rank decides order; if one word runs out first, it must be the prefix (comes first). If every adjacent pair passes, the whole list is sorted.

COACH'S BOARD — THE PATTERN, STEP BY STEP
THE RANK CHECKPATTERN · RANK ARRAY, ONE PASSwords = [cat, bat, apple] · order = "cbadefg...z"
cat
bat
apple
RANK TABLE — LETTER → POSITION
— empty —
STEP 1

order = "cbadefghijklmnopqrstuvwxyz" — a reshuffled alphabet. Build a 26-slot rank table from it once.

STEP 1 / 6 · ← → WORK TOO
THE SPLITS — TWO PACES, TWO LANGUAGES
grind/verifying-an-alien-dictionary.pyRACE PACE
LANG ▸
PACE ▸
class Solution:
    def isAlienSorted(self, words: List[str], order: str) -> bool:
        rank = {c: i for i, c in enumerate(order)}

        def to_ranks(w):
            return [rank[c] for c in w]

        for i in range(len(words) - 1):
            if to_ranks(words[i]) > to_ranks(words[i + 1]):
                return False
        return True
TIME O(N·L)SPACE O(1)PYTHON · RACE PACE · 11 LN

✓ CHIP-TIMED — ALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED