Open the Lock and Return a Shortest Path
Problem statement
A lock has four circular wheels, and each wheel contains the digits 0 through 9. In one move, you may turn exactly one wheel forward or backward by one digit. Turning forward from 9 produces 0, and turning backward from 0 produces 9.
Given a starting combination start, a desired combination target, and an array blocked of combinations that the lock may never enter, return a shortest valid sequence of combinations from start through target, inclusive.
If several shortest paths exist, return the lexicographically smallest sequence. If no valid path exists, return an empty array.
Function
shortestLockPath(start: String, target: String, blocked: String[]) → String[]Examples
Example 1
start = "0000"target = "0202"blocked = ["0201","0101","0102","1212","2002"]return = ["0000","0001","0002","0003","0103","0203","0202"]The sequence uses six moves, which is minimum. Among all six-move paths, this sequence is lexicographically smallest.
Example 2
start = "0000"target = "0009"blocked = []return = ["0000","0009"]Turning the last wheel backward wraps directly from 0 to 9.
Example 3
start = "0000"target = "0000"blocked = ["0000"]return = []The starting combination is blocked, so no valid path exists.
Constraints
start.length == target.length == 4.- Every combination contains only digits from
0through9. 0 <= blocked.length <= 10000.- All strings in
blockedare distinct.