Watch 10 video solutions for Word Ladder II, a hard level problem involving Hash Table, String, Backtracking. This walkthrough by Techdose has 52,755 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that:
si for 1 <= i <= k is in wordList. Note that beginWord does not need to be in wordList.sk == endWordGiven two words, beginWord and endWord, and a dictionary wordList, return all the shortest transformation sequences from beginWord to endWord, or an empty list if no such sequence exists. Each sequence should be returned as a list of the words [beginWord, s1, s2, ..., sk].
Example 1:
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"] Output: [["hit","hot","dot","dog","cog"],["hit","hot","lot","log","cog"]] Explanation: There are 2 shortest transformation sequences: "hit" -> "hot" -> "dot" -> "dog" -> "cog" "hit" -> "hot" -> "lot" -> "log" -> "cog"
Example 2:
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"] Output: [] Explanation: The endWord "cog" is not in wordList, therefore there is no valid transformation sequence.
Constraints:
1 <= beginWord.length <= 5endWord.length == beginWord.length1 <= wordList.length <= 500wordList[i].length == beginWord.lengthbeginWord, endWord, and wordList[i] consist of lowercase English letters.beginWord != endWordwordList are unique.105.Problem Overview: You are given a beginWord, an endWord, and a dictionary of valid words. Each transformation changes exactly one character and must exist in the dictionary. The task is to return all shortest transformation sequences from the start word to the target word.
Approach 1: Classic Breadth-First Search + Backtracking (O(N * L * 26) time, O(N * L) space)
This approach models the dictionary as an implicit graph where each word connects to words that differ by one character. Use Breadth-First Search starting from beginWord to explore transformations level by level. BFS guarantees the first time you reach endWord is the shortest depth. During traversal, store parent relationships in a map so each word keeps track of the words that led to it at the previous level. Instead of stopping immediately when the target appears, finish the entire BFS layer to capture all shortest paths. After BFS builds the parent graph, run a backtracking DFS from endWord to beginWord to reconstruct every valid shortest sequence. The dictionary lookup is optimized using a hash table. The complexity comes from generating up to 26 * L neighbors for each word.
Approach 2: Bidirectional Breadth-First Search (O(N * L * 26) time, O(N * L) space)
Bidirectional BFS improves practical performance by expanding the search simultaneously from beginWord and endWord. Instead of exploring the entire search space from one side, the algorithm always expands the smaller frontier. When the two frontiers meet, the shortest transformation length is determined. While expanding nodes, build adjacency relationships that record how words connect between levels. After the search completes, run a DFS or backtracking step to enumerate all valid sequences using the recorded transitions. This reduces the number of explored states significantly compared with single-direction BFS, especially when the dictionary is large. The time complexity remains proportional to neighbor generation (N * L * 26), but the explored portion of the graph is often much smaller.
Recommended for interviews: The expected solution combines BFS for shortest path discovery and backtracking to reconstruct all valid sequences. Interviewers want to see that you understand why BFS guarantees minimal transformations and how to store parent relationships for path reconstruction. Bidirectional BFS demonstrates deeper optimization skills and is preferred when the dictionary size is large.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Classic BFS + Backtracking | O(N * L * 26) | O(N * L) | Standard solution for interviews. Clear logic for finding shortest paths and reconstructing sequences. |
| Bidirectional BFS | O(N * L * 26) | O(N * L) | Best when the dictionary is large. Expands from both ends to reduce the explored search space. |