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.
The idea is to optimize the BFS by starting the search from both the beginWord and the endWord simultaneously. This bidirectional approach helps in cutting down the search space significantly as the two searches will meet in the middle.
Initially, we create two sets to track words to visit from the beginning side and the ending side. We also use a visited set to ensure we do not revisit any words.
We alternate the search extensions from one side to another to maintain the balance of the search width for the two meet points.
Due to the bidirectional nature, the time complexity can be greatly reduced compared to a conventional BFS.
This Python code uses a bidirectional approach to construct all possible paths from the beginning to the end word. The set operations ensure efficient word lookups and deletions, while the recursive helper function constructs all paths once the tree is established.
JavaScript
Java
Time Complexity: O(N * M * 26) where N is the number of words and M is the word length.
Space Complexity: O(N * M) due to the storage of the graph.
Using a single queue BFS, we explore transformations level by level. We build a graph where each word is a node, and edges are valid single-letter transformations as they appear in the word list. During BFS, paths are stored and checked for minimum length.
We mark words visited in the current level to avoid loops and unnecessary processing.
Once a complete path is found its length is compared to ensure it’s the smallest, and added to results if valid.
This C code uses a loop-based BFS leveraging basic queue operations to explore each word's path to potential matches. A helper function ensures only one-letter transformations are pursued. The transformation check and BFS implementation are in C style for maximum control over memory and data handling.
C++
C#
Time Complexity: O(N * M * 26).
Space Complexity: O(N * M).
| Approach | Complexity |
|---|---|
| Bidirectional Breadth-First Search | Time Complexity: O(N * M * 26) where N is the number of words and M is the word length. |
| Classic Breadth-First Search (BFS) | Time Complexity: O(N * M * 26). |
| 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. |
G-30. Word Ladder - 2 | Shortest Paths • take U forward • 173,292 views views
Watch 9 more video solutions →Practice Word Ladder II with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor