Skip to content
Decoder

Word Ladder

Hard

Problem

You want to turn begin into end by changing one letter at a time, and every intermediate word (and end itself) must appear in word_list. Return the number of words in the shortest such sequence, counting both begin and end. Return 0 if it cannot be done.

Example 1

begin = "red", end = "tax",

Example 2

word_list = ["ted", "tex", "red", "tax", "tad", "den", "rex", "pee"]

Example 3

  →  4   (red → ted → tad → tax)

Example 4

begin = "hot", end = "dog", word_list = ["hot", "dog"]

Example 5

  →  0   (no word sits one letter away from both)

Constraints

  • 1 <= begin.length <= 10 and every word has the same length as begin
  • 1 <= word_list.length <= 5000
  • Words use lowercase English letters only, begin != end, and begin does not need to be in the list.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.