Grokking Data Structures & Algorithms for Coding Interviews
Vote

0% completed

Solution: Open the Lock

Problem Statement

A four wheel lock starts at "0000". One move turns one wheel one step in either direction, and the wheels wrap around. Some settings are dead ends. Return the smallest number of moves that reaches target, or -1.

Examples

Example 1

  • Input: deadends = ["0201", "0101", "0102", "1212", "2002"], target = "0202"
  • Expected Output: 6

Example 2

  • Input: deadends = ["8888"], target = "0009"
  • Expected Output: 1

Example 3

  • Input: deadends = ["0000"], target = "8888"
  • Expected Output: -1

.....

.....

.....

Like the course? Get enrolled and start learning!

Reading Progress

0%


Vote for new content