Word Ladder II
Master this topic with zero to advance depth.
Word Ladder II
Given two words, beginWord and endWord, and a dictionary wordList, return all shortest transformation sequences from beginWord to endWord. Only one letter can be changed at a time.
Examples
Level I: Standard BFS (Shortest Transformation)
Intuition
Use a standard BFS to find the shortest distance from beginWord to endWord. In each step, change one character at a time and check if it exists in the dictionary. This approach only finds the length of the shortest path, not the actual paths themselves.
Level III: BFS + Backtracking (Trie for Neighbors)
Intuition
Use BFS to find the shortest distance from beginWord to all reachable words. To quickly find 'neighbors' (words differing by one char), we can iterate through each position of the word and try all 26 letters, checking against a Set/Trie. Finally, use DFS to backtrack all shortest paths from beginWord to endWord using the distance levels found in BFS.
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.