Skip to main content
Hard Queue / BFS Medium frequency

Word Ladder

Open on LeetCode

Approach Summary

BFS where each step transforms one character. Use a word set for O(1) neighbor lookup. BFS guarantees shortest path.

Full Solution & Approach

Each transformation changes exactly one character, so the words form a graph where edges connect words differing by one letter, and the answer is the shortest path from beginWord to endWord — BFS. Put the word list in a set for O(1) membership checks. BFS from beginWord; at each word, generate every one-letter variant by substituting each of 26 letters at each position, and if a variant is in the word set and unvisited, enqueue it with the next step count. The step count is incremented per BFS level, which is exactly the transformation length. The branching factor is bounded by word_length × 26, so the total work is O(n · m² · 26) in the worst case. Because BFS explores by distance, the first time endWord is reached is guaranteed to be the minimum number of transformations.

BFS visits at most every word once, expanding each into m × 26 candidates checked in O(m) — O(n · m² · 26) worst case. The visited set and queue hold up to n words — O(n · m).

Solution Code

Solution

from collections import deque

def ladder_length(begin_word: str, end_word: str, word_list: list[str]) -> int:
    words = set(word_list)
    if begin_word == end_word:
        return 0
    if end_word not in words:
        return 0
    queue = deque([(begin_word, 1)])
    visited = {begin_word}
    while queue:
        word, steps = queue.popleft()
        if word == end_word:
            return steps
        for i in range(len(word)):
            for c in 'abcdefghijklmnopqrstuvwxyz':
                nxt = word[:i] + c + word[i + 1:]
                if nxt in words and nxt not in visited:
                    visited.add(nxt)
                    queue.append((nxt, steps + 1))
    return 0

Edge Cases to Watch

  • beginWord equals endWord — return 0
  • endWord not in the word list — return 0
  • No path exists — return 0
  • Words of length 1 — the branching factor is tiny and the search is fast

How to Recognize This Pattern

  • Shortest transformation sequence
  • One character change per step

Complexity Analysis

Time Complexity

O(n · m² · 26)

Space Complexity

O(n · m)

Tags

Hash Set BFS String

This site is free. If these guides are helping your prep, consider buying me a coffee. ☕

Support →
Buy me a coffee