Watch 10 video solutions for Word Ladder, a hard level problem involving Hash Table, String, Breadth-First Search. This walkthrough by take U forward has 271,735 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 the number of words in the shortest transformation sequence from beginWord to endWord, or 0 if no such sequence exists.
Example 1:
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"] Output: 5 Explanation: One shortest transformation sequence is "hit" -> "hot" -> "dot" -> "dog" -> cog", which is 5 words long.
Example 2:
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"] Output: 0 Explanation: The endWord "cog" is not in wordList, therefore there is no valid transformation sequence.
Constraints:
1 <= beginWord.length <= 10endWord.length == beginWord.length1 <= wordList.length <= 5000wordList[i].length == beginWord.lengthbeginWord, endWord, and wordList[i] consist of lowercase English letters.beginWord != endWordwordList are unique.Problem Overview: Given a beginWord, an endWord, and a dictionary of words, compute the length of the shortest transformation sequence where only one character can change at a time and every intermediate word must exist in the dictionary.
Approach 1: Explicit Graph Construction + BFS (O(N^2 * M) time, O(N^2) space)
Treat each word as a node in a graph. Two words share an edge if they differ by exactly one character. You build the graph by comparing every pair of words in the dictionary and connecting valid transformations. After the graph is constructed, run breadth-first search starting from beginWord to find the shortest path to endWord. BFS guarantees the shortest path in an unweighted graph. This approach is easy to reason about but expensive because checking every pair requires O(N^2) comparisons, each costing O(M) for word length.
Approach 2: BFS with Word Transformations (O(N * M * 26) time, O(N) space)
Instead of building the entire graph, generate neighbors on the fly. Store the dictionary in a hash table for constant-time lookup. During BFS, take the current word and try replacing each character position with all 26 lowercase letters. Each generated string represents a possible one-step transformation. If the new word exists in the dictionary and hasn't been visited, push it into the BFS queue and mark it visited. Each word has M positions and 26 possible replacements, so neighbor generation costs O(M * 26). Since each word is processed once, the total time becomes O(N * M * 26). This avoids building the full graph and is the standard optimized solution.
Approach 3: Bidirectional BFS (O(N * M * 26) time, O(N) space)
Run two BFS searches simultaneously: one from beginWord and one from endWord. At each step, expand the smaller frontier to keep the search balanced. When the two searches meet, the transformation path is found. This dramatically reduces the number of explored states in large dictionaries. Neighbor generation is still done with character replacements using string manipulation, but the search depth from each side is roughly half, which improves practical runtime.
Recommended for interviews: BFS with on-the-fly word transformations. Interviewers expect you to model the problem as an unweighted shortest path and apply BFS. Building neighbors dynamically with a hash set demonstrates awareness of graph optimization. Bidirectional BFS is a strong follow-up optimization once the standard BFS solution is established.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Explicit Graph Construction + BFS | O(N^2 * M) | O(N^2) | Small dictionaries where building the full graph is acceptable |
| BFS with Word Transformations | O(N * M * 26) | O(N) | General case and most interview solutions |
| Bidirectional BFS | O(N * M * 26) | O(N) | Large word lists where reducing BFS depth improves performance |