BFS five letter word chain
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A five-letter word chain is a shortest-path problem in disguise. Each word is a node, two words are connected if they differ by exactly one letter, and breadth-first search is the natural algorithm because it finds the shortest path in an unweighted graph.
Model the Puzzle as a Graph
Suppose you want to transform one five-letter word into another by changing one letter at a time, always staying inside a valid dictionary. For example:
- start word:
stone - end word:
money - valid intermediate words must also be real five-letter words
That is a graph problem:
- each word is a vertex
- an edge exists when two words differ by one character
- the desired chain is the shortest path from start to target
Because every edge has equal cost, BFS is the correct shortest-path algorithm.
A Direct BFS Solution
A straightforward implementation generates neighbors by trying all one-letter substitutions and checking whether the result is in the dictionary set.
This works because BFS explores all words at distance 1, then all words at distance 2, and so on. The first time it reaches the target, the discovered chain is shortest.
Why BFS Is the Right Choice
Depth-first search can find a chain, but it does not guarantee the shortest one. BFS does, as long as every step counts equally.
That makes BFS ideal for word-ladder or word-chain puzzles because each letter change is just one move. You are not optimizing weighted cost or probability. You are minimizing the number of transformations.
Reconstructing the Path
The parent dictionary is the key to turning BFS from a yes-or-no reachability test into an actual chain generator. Every time BFS discovers a new word, it records which previous word led to it.
Once the target is found, walking backward through parent reconstructs the path. Without that record, BFS could still tell you the distance, but not the actual transformation chain.
Performance Considerations
The naive neighbor generator is often fine for small puzzles, but large dictionaries can make it expensive because each visited word tries many candidate mutations.
A common optimization is to precompute wildcard buckets. For example, stone contributes patterns such as:
- '
_tone' - '
s_one' - '
st_ne' - '
sto_e' - '
ston_'
Words that share a wildcard pattern differ by one letter at one position, so those buckets make neighbor lookup much faster.
That optimization is not required to understand BFS, but it matters if the dictionary is large.
Common Pitfalls
Using depth-first search when the real requirement is the shortest chain can produce correct-looking but unnecessarily long paths.
Forgetting to mark visited words allows the search to revisit the same nodes and can create massive slowdowns or loops.
Not storing parent pointers means you can discover the target but still fail to reconstruct the chain itself.
Allowing words of mixed length breaks the graph model because the one-letter-change rule assumes a consistent word size.
Treating the dictionary as a list instead of a set makes membership checks much slower than they need to be.
Summary
- A five-letter word chain is naturally modeled as an unweighted graph.
- BFS is the right algorithm because it finds the shortest path.
- Use a queue for traversal and a parent map to reconstruct the chain.
- Store the dictionary in a set for fast membership checks.
- Precomputed wildcard buckets can speed up neighbor lookup on large word lists.

