752. Open The Lock
Practice Open The Lock for free on NudgeCode: Medium, Graph pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.
A lock has 4 wheels, each showing a digit 0-9. One move turns one wheel by one step up or down, and the wheels wrap around (9 -> 0 and 0 -> 9). The lock starts at "0000". If it ever shows a code in deadends, it jams and cannot move again. Return the minimum number of moves to reach target, or -1 if it is impossible.
Example
deadends = ["0201","0101","0102","1212","2002"], target = "0202" -> 6
Constraints
- 0 <= deadends.length <= 500
- deadends[i] and target are 4-digit strings
targetis not indeadends