Watch 10 video solutions for Word Ladder II, a hard level problem involving Hash Table, String, Backtracking. This walkthrough by take U forward has 173,292 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: Given a beginWord, endWord, and a dictionary of valid words, find all shortest transformation sequences from begin to end. Each step changes exactly one character and every intermediate word must exist in the dictionary.
Approach 1: Classic Breadth-First Search + Backtracking (O(N * L * 26 + P * L) time, O(N * L) space)
The shortest path requirement immediately points to Breadth-First Search. Start BFS from beginWord and generate neighbors by replacing each character with a-z. Store valid transformations in a dictionary set for O(1) lookups using a Hash Table. While exploring levels, record parent relationships instead of full paths. Each word keeps a list of previous words that can reach it at the same shortest distance.
Once BFS reaches endWord, stop expanding deeper levels because longer paths are not needed. Use Backtracking from endWord to beginWord through the parent map to reconstruct every valid shortest sequence. BFS guarantees minimal distance, while the parent graph preserves all branching paths that achieve that distance.
This approach is straightforward and reliable for interviews. The main cost comes from generating neighbors (L * 26 mutations per word) and building multiple paths during backtracking.
Approach 2: Bidirectional Breadth-First Search (O(N * L * 26 + P * L) time, O(N * L) space)
Bidirectional BFS reduces search space by expanding from both beginWord and endWord simultaneously. Instead of exploring the entire graph from one side, two frontiers grow toward each other. At every step, expand the smaller frontier to keep branching minimal. This significantly cuts the number of transformations explored in dense dictionaries.
Maintain a graph that records connections between levels depending on search direction. When the two frontiers meet, stop BFS because the shortest distance is known. After building the adjacency relationships, run DFS or backtracking from beginWord to generate every valid sequence.
The key advantage is reduced branching. For large dictionaries, the search depth shrinks roughly in half, making it far faster than classic BFS. Implementation is slightly more complex because direction changes affect how edges are recorded.
Recommended for interviews: Start with the classic BFS explanation since it clearly demonstrates shortest-path reasoning and graph traversal fundamentals. Once that is clear, mention bidirectional BFS as the optimization interviewers expect for large datasets. Showing both approaches signals strong understanding of BFS graph problems and performance tradeoffs.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Classic BFS + Backtracking | O(N * L * 26 + P * L) | O(N * L) | Standard interview solution. Clear logic and easier to implement. |
| Bidirectional BFS | O(N * L * 26 + P * L) | O(N * L) | Large dictionaries where reducing BFS search space significantly improves performance. |