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

Word Ladder II

HardBacktracking

You get a start word beginWord, a target word endWord and a list of allowed words wordList. All words have the same length. A ladder is a sequence of words that starts at beginWord and ends at endWord, where each word differs from the one before it in exactly one letter, and every word after the first comes from wordList.

Return every ladder of the smallest possible length, each as a list of words, in any order. If no ladder exists, return an empty list. beginWord itself does not have to be in wordList.

Example 1

Input:  beginWord = "cat", endWord = "dog",
        wordList = ["cot", "cog", "dot", "dog", "cag"]
Output: [["cat","cot","dot","dog"], ["cat","cot","cog","dog"],
         ["cat","cag","cog","dog"]]
Why:    three ladders of 4 words; none is shorter
2 more examples and the constraints

Example 2

Input:  beginWord = "cat", endWord = "dog",
        wordList = ["cot", "cog", "dot", "cag"]
Output: []
Why:    dog is not an allowed word, so no ladder can end there

Example 3

Input:  beginWord = "ab", endWord = "cd", wordList = ["cb", "cd", "ad"]
Output: [["ab","ad","cd"], ["ab","cb","cd"]]
Why:    change either letter first

Constraints

  • 1 ≤ word length ≤ 5, and all words have the same length
  • 1 ≤ length of wordList ≤ 500; its words are distinct
  • Only lowercase English letters; beginWord != endWord
  • All shortest ladders together hold at most 100,000 words

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. BFS that carries whole pathsO(paths · L)
  2. BFS with parents, then backtrackO(n · L · 26 + P)

The idea in one sentence

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

Side by side

ApproachTimeRelative work on a dense list of 500 wordsVerdict
BFS that carries whole pathsO(paths · L)
Copies paths; can blow up
BFS with parents, then backtrackO(n · L · 26 + P)
Best; each word expanded once

n = words in the list, L = word length, P = total words in all shortest ladders.

Edge cases to test

  • End word not allowed. If endWord is missing from wordList, return [] at once.
  • Unreachable end. The BFS runs out of words before reaching the end: return [].
  • Begin word in the list. It is already in parents, so it is never reached again as a later step.
  • One step. hen to pen with pen allowed is the single ladder [hen, pen].
  • Many ladders. With every word of a's and c's of length 5 allowed, there are 5! = 120 ladders.

Common mistakes

  • Marking a word visited as soon as it is first reached, which drops its other parents in the same layer.
  • Continuing the BFS past the layer that reached endWord, which adds longer ladders.
  • Running a DFS over the whole word graph instead of the parent links, which times out.

How to talk through it

  1. 1Clarify. All shortest ladders, in any order? Does the begin word count as part of the list? What if the end word is missing?
  2. 2Model it as a graph. Words are nodes, one-letter changes are edges, and shortest means BFS.
  3. 3Explain the naive cost. Carrying whole paths in the BFS copies shared prefixes again and again.
  4. 4Keep parents instead. BFS stores each word's parents in the previous layer; a DFS from the end lists the ladders.
  5. 5Test out loud. Try the missing end word, a one-step ladder, and a word reached from two parents.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?