Word Ladder
BFS over words: one letter per step, shortest sequence wins
Treat every word as a vertex and every one-letter change as an edge, and the problem becomes the shortest path in an unweighted graph — which is breadth-first search. Nobody builds that graph up front: from each word, swap every position for every letter a to z and keep the candidates that are still in the word set. Erasing a word from the set the moment it is discovered is what keeps the search linear, because the first route to reach a word is already the shortest, so no later route needs it. The badge on each node is the ladder length that reached it.
Details
Animated on: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"] — length of the shortest transformation sequence, or 0.
Problem:Word LadderHard
BFS over words: one letter per step, shortest sequence wins
step 1 / 10
Every one-letter change is an edge
"hit" must become "cog" one letter at a time, and every intermediate word has to be in the list. Each line here joins two words that differ in exactly one letter — so the question is the shortest path in an unweighted graph, and that is exactly what breadth-first search finds.
1FUNCTION ladderLength(beginWord, endWord, wordList):2 dict <- SET OF wordList3 IF endWord NOT IN dict RETURN 04 queue <- [(beginWord, 1)]5 WHILE queue NOT EMPTY6 (word, steps) <- REMOVE FIRST FROM queue7 IF word = endWord RETURN steps8 FOR i FROM 0 TO LENGTH(word) - 19 FOR EACH c IN 'a' TO 'z'10 next <- word WITH word[i] REPLACED BY c11 IF next IN dict12 REMOVE next FROM dict13 APPEND (next, steps + 1) TO queue14 RETURN 0
← / → step · space play · Home restart
Every step in words
Show all 10 steps
- "hit" must become "cog" one letter at a time, and every intermediate word has to be in the list. Each line here joins two words that differ in exactly one letter — so the question is the shortest path in an unweighted graph, and that is exactly what breadth-first search finds.
- First check "cog" is in the list of 6 words — it is, so a ladder is possible. Without this check the search would run to exhaustion for nothing.
- Queue "hit" with ladder length 1 — the sequence counts words, not changes, so the start word alone is already a ladder of length 1.
- Take "hit" (badge 1) and try all 26 letters in each of its 3 positions — 78 candidates, but only "hot" is still in the set. It gets badge 2 and is erased from the set, so no later, longer route can ever re-add it. Queue: "hot".
- Take "hot" (badge 2) and try all 26 letters in each of its 3 positions — 78 candidates, but only "dot" and "lot" are still in the set. They get badge 3 and are erased from the set, so no later, longer route can ever re-add them. Queue: "dot", "lot".
- Take "dot" (badge 3) and try all 26 letters in each of its 3 positions — 78 candidates, but only "dog" is still in the set. It gets badge 4 and is erased from the set, so no later, longer route can ever re-add it. Queue: "lot", "dog".
- Take "lot" (badge 3) and try all 26 letters in each of its 3 positions — 78 candidates, but only "log" is still in the set. It gets badge 4 and is erased from the set, so no later, longer route can ever re-add it. Queue: "dog", "log".
- Take "dog" (badge 4) and try all 26 letters in each of its 3 positions — 78 candidates, but only "cog" is still in the set. It gets badge 5 and is erased from the set, so no later, longer route can ever re-add it. Queue: "log", "cog".
- Take "log" (badge 4) and try every one-letter change — nothing left in the set matches, because every neighbour was already claimed by an earlier, shorter route. Queue: "cog".
- The word leaving the queue is "cog" itself, with badge 5. BFS grows one level at a time, so the first time the end word comes off the queue its level is the shortest possible: hit -> hot -> dot -> dog -> cog. RETURN 5.
Now solve it yourself
Watching is the first half. The second is writing it without the animation.