LeetCode #127 Hard

Word Ladder

Find the length of the shortest transformation sequence from beginWord to endWord, changing one letter at a time through words in wordList.

bfsimplicit-graphstrings
Open on LeetCode ↗
02

Intuition

💡

It is tempting to compare every word against every other word to decide which pairs are one letter apart, but that is O(n^2 L) before the BFS even starts. Instead, never build the graph at all: for the current word, try swapping in each of the 26 letters at each position, and check whether the result is in the word set. That generates exactly the neighbors that matter, on demand, in O(L 26) per word. The graph is implicit - it exists only as a rule for generating edges, computed lazily as BFS needs them.

03

Approach

1

Guard the target

If endWord is not in wordList, no sequence of legal one-letter swaps can ever land on it, so return 0 immediately.

2

Generate neighbors on demand

From the current word, for each position and each of the 26 letters, build the candidate word. If it is in the word set and unvisited, it is a genuine neighbor - no precomputed adjacency list needed.

3

BFS layer = transformation count

Track distance from beginWord (starting at 1). The first time endWord is generated, its distance is the answer, since BFS explores shortest sequences first.

04

Solution & live demo

python
1class Solution:
2 def ladderLength(self, beginWord, endWord, wordList):
3 from collections import deque
4 words = set(wordList)
5 if endWord not in words:
6 return 0
7 q = deque([(beginWord, 1)])
8 words.discard(beginWord)
9 while q:
10 word, d = q.popleft()
11 if word == endWord:
12 return d
13 for i in range(len(word)):
14 for ch in 'abcdefghijklmnopqrstuvwxyz':
15 cand = word[:i] + ch + word[i+1:]
16 if cand in words:
17 words.discard(cand)
18 q.append((cand, d+1))
19 return 0
05

Edge cases

endWord not in wordList

Return 0 right away, no BFS needed.

beginWord already equals endWord

Not a valid LeetCode input, but would resolve trivially.

wordList has unrelated words

They are simply never generated as neighbors and never visited.

No transformation path exists

BFS queue empties without reaching endWord; return 0.

06

Complexity

Time
O(n * L * 26)
Space
O(n * L)
n = wordList size, L = word length; no pairwise comparisons ever built.