‹ back to the board

Friday, September 25

Adeptalgorithms & data structures

The Gate That Reads the Wall

The warden's gate in bitwick stands beside a long wall of rune-marks. The wall is given as runes, a string of lowercase letters, read left to right. The gate's charm is given as charm, a string of lowercase letters; the charm is not a pattern to match but a bag of marks the gate demands, and repeats count — if the charm names a letter three times, the gate wants that letter three times.

The warden opens the gate by chalking a bracket around one unbroken run of consecutive marks on the wall. A run satisfies the gate when, counted with multiplicity, it contains every letter of the charm at least as many times as the charm names it. Extra marks inside the run are harmless, and the marks may sit in any order.

Write shortestCharm(runes, charm), which returns the shortest run of consecutive characters of runes that satisfies the charm, as a string. If several runs tie for shortest, return the one that begins earliest in runes. If no run satisfies the charm, return the empty string. If charm is empty, return the empty string.

  • two pointers and sliding windows
  • Adept

examples

  1. case 1runes of "adobecodebanc", charm of "abc""banc" - the run starting at index 9; every shorter run is missing at least one of a, b, c
  2. case 2runes of "abbcab", charm of "aab""abbca" - the charm wants two a's, so the run must reach both of them
  3. case 3runes of "aa", charm of "aaa""" - the wall never holds three a's at once
reward
320 XP
signature
shortestCharm
not yet run

TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.

loading the editor…
  • classic wall
  • charm with a repeated letter
  • earliest of tied shortest runs
  • shortest run sits late on the wall
  • a run of four beats the obvious one
  • wall cannot satisfy the charm
  • empty charm
  • blank wall
  • single mark suffices
The Gate That Reads the Wall · bitwick