hardGraphsFacebookAmazonLinkedIn
Word-Ladder Transformer
A spelling game morphs one word into another by changing one letter at a time; each intermediate must come from a given dictionary.
Given beginWord, endWord, and a wordList, return the length of the shortest transformation sequence, or 0 if none.
Choose approach
hit
0cur
hot
1dot
2dog
3lot
4log
5cog
6SetQueue (0)
empty
1/8
Narrationstep 1 of 8
Dequeue "hit" at depth 1.
Variables
word=hit
depth=1
queue=0
How to think about it
- BFS depth = ladder length.
- Delete visited words from the dictionary to avoid loops.
- Bidirectional BFS halves the search tree in practice.
Edge cases to catch in interview
- endWord not in list -> 0.
- beginWord == endWord usually constrained away.
Examples
Example 1
5{"begin":"hit","end":"cog","wordList":["hot","dot","dog","lot","log","cog"],"values":["hit","hot","dot","dog","lot","log","cog"]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed