FunctionRacerProblemsRace

Order of a Strange Alphabet

HardGraphsPython, JavaScript, TypeScript, Java, C++

A strange language uses some of our lowercase letters, but in an order of its own. words is a list of its words, sorted the way a dictionary of that language sorts them: by the first letter that differs, and a word comes after every shorter word it starts with.

Return the letters that appear in words, each once, in an order that agrees with the list. If several orders agree, return the one that comes first in our own alphabetical order. If no order agrees, return an empty string.

Example 1

words ["ba","bc","ac","cab"]
returns "bac"

ba before bc puts a before c. bc before ac puts b before a. That fixes the order b, a, c.

Example 2

words ["ca","cb"]
returns "abc"

The only rule is a before b. The letter c is free and goes as early as our alphabet allows: after a and b.

Example 3

words ["abc","ab"]
returns ""

A word cannot come before its own beginning, so no order fits.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Graphs problems