Skip to the code
cf.
+25 XP

#441 Stylesheet Load Order

medium

A theme system loads a list of stylesheets, given by name in sheets. Each rule in rules is a pair [first, second] meaning stylesheet first must load before second, so that second can override it.

Write loadOrder(sheets, rules) that returns every stylesheet name in a valid load order. Whenever several stylesheets are ready to load, take the one whose name comes first in plain string order (the order sort() uses). If the rules contain a circle, return [].

Example 1

Input: sheets = ["reset.css","base.css","theme.css","app.css"], rules = [["reset.css","base.css"],["base.css","app.css"],["theme.css","app.css"]]
Output: ["reset.css","base.css","theme.css","app.css"]

reset.css and theme.css are ready first, and reset.css sorts first. That frees base.css, which sorts before theme.css. app.css waits for both.

Example 2

Input: sheets = ["a.css","b.css"], rules = [["a.css","b.css"],["b.css","a.css"]]
Output: []

Each must load before the other, which is impossible.

Constraints

  • 1 <= sheets.length <= 1000
  • Names are unique.
  • 0 <= rules.length <= 5000
  • Every name in rules is in sheets.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).