Gamified challenge · Breadth-first search
A lock has four wheels, each showing a digit 0–9, and starts at "0000". One turn moves one wheel one notch up or down, and the wheels wrap round (9 up is 0, 0 down is 9). Every combination in deadends jams the lock: it may never be shown, not even for a moment.
Write openLock(deadends, target) that returns the fewest turns to show target, or -1 if it can't be done (for example when 0000 or the target is itself a jam). If target is "0000", the answer is 0.
Example: with deadends = ["8888"] and target = "0009", turn the last wheel down once: the answer is 1.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google