Skip to content
Problem 752 · LeetCode 752(opens in a new tab)

Open the Lock

MediumBreadth-first search

A lock has 4 wheels, each showing a digit from 0 to 9. One move turns a single wheel one notch, up or down, and the wheels wrap: 9 turns up to 0 and 0 turns down to 9. The lock starts showing 0000.

You are given deadends, a list of 4-digit codes. If the lock ever shows one of them, it jams for good. Return the fewest moves that make the lock show target without ever showing a dead end, or −1 if that cannot be done.

Example 1

Input:  deadends = ["1000","9000","0100","0900","0010","0090"],
        target = "0002"
Output: 2
Why:    only the last wheel can move: 0000 → 0001 → 0002
2 more examples and the constraints

Example 2

Input:  deadends = ["0010","0001"], target = "0011"
Output: 4
Why:    0000 → 1000 → 1010 → 1011 → 0011 goes around both

Example 3

Input:  deadends = ["4555","6555","5455","5655",
                    "5545","5565","5554","5556"],
        target = "5555"
Output: -1
Why:    every code next to 5555 is a dead end

Constraints

  • 1 ≤ length of deadends ≤ 500
  • Every dead end and the target are 4 digits long
  • The target is never one of the dead ends

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Breadth-first searchO(10⁴ · 8)
  2. Search from both endsO(10⁴ · 8)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for a target 8 turns awayVerdict
Breadth-first searchO(10⁴ · 8)
Passes; the standard answer
Search from both endsO(10⁴ · 8)
Best; visits far fewer codes

10⁴ = the number of possible codes; each has 8 neighbours.

Edge cases to test

  • 0000 is a dead end. The lock is jammed from the start: return −1.
  • Target is 0000. No moves needed: return 0 (as long as 0000 is not dead).
  • Target walled in. If all 8 neighbours of the target are dead ends, it can never be reached.
  • Wrap-around. Target 0009 is one move away, turning the last wheel down from 0.
  • Detours. Dead ends on the direct route can force a longer path, as in Example 2.

Common mistakes

  • Forgetting that wheels wrap, so 0 never turns down to 9.
  • Marking codes seen when they are popped instead of when they are queued, which queues the same code many times.
  • Not checking whether 0000 itself is a dead end.

How to talk through it

  1. 1Clarify. Do the wheels wrap? Can 0000 be a dead end? Can the target be 0000?
  2. 2Model it as a graph. 10,000 codes, each joined to 8 others by one turn; dead ends are removed nodes.
  3. 3Pick BFS. Every move costs 1, so BFS reaches each code by the fewest moves first.
  4. 4Mention both ends. Searching from 0000 and the target at once and growing the smaller side cuts the codes visited.
  5. 5Test out loud. Run 0000 dead, target 0000, a walled-in target and a wrap-around target.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?