Skip to content

Word Ladder

HardGraphs
Decode it first. Clues, constraints, pattern and plan before any code.

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.

begin = "red", end = "tax",
word_list = ["ted", "tex", "red", "tax", "tad", "den", "rex", "pee"]
  →  4   (red → ted → tad → tax)
 
begin = "hot", end = "dog", word_list = ["hot", "dog"]
  →  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.