The Mask-Pillars of the Tzallow Jungle
Deep in the Tzallow Jungle runs a stone causeway lined with carved pillars, each bearing the mask of a single beast-spirit. The Order of the Green Lantern pays well for a warding rite performed there, but the rite only holds if the warden stands within an unbroken stretch of pillars whose masks together satisfy the rite's demands. The warden wants the shortest such stretch, so there is less ground to guard.
Write shortestRite(pillars, rite).
pillarsis an array of strings: the mask on each pillar, in order along the causeway. It may be empty.riteis an array of strings: the masks the rite demands. A mask may appear more than once. If it appearsctimes inrite, the stretch must contain at leastcpillars bearing that mask.
A *stretch* is one or more consecutive pillars. It satisfies the rite if, for every distinct mask in rite, the number of pillars in the stretch bearing that mask is at least the number of times that mask appears in rite. Extra pillars, and masks the rite does not mention, are allowed. Masks are compared as exact strings.
Return an integer: the number of pillars in the shortest stretch that satisfies the rite. If rite is empty, return 0. If no stretch satisfies the rite, return -1.
examples
- case 1
pillars of ["owl", "bat", "jaguar", "owl", "owl", "bat"], rite of ["owl", "owl", "bat"]3 - the last three pillars (owl, owl, bat) hold two owls and a bat - case 2
pillars of ["jaguar", "owl", "serpent", "owl", "jaguar", "bat", "serpent"], rite of ["serpent", "jaguar"]3 - no two neighbouring pillars hold both a serpent and a jaguar - case 3
pillars of ["owl", "bat"], rite of ["owl", "owl"]-1 - the causeway has only one owl, and the rite demands two
- reward
- 320 XP
- signature
shortestRite
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- multiplicity at the end
- two masks never adjacent
- not enough of a repeated mask
- empty rite
- empty causeway
- single pillar matches
- whole causeway needed
- mask absent from causeway
- shorter stretch found after a long one
- surplus repeats inside window
- exact string comparison