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.
This approach leverages Breadth First Search (BFS), where each word is a node in the graph, and an edge exists between two nodes if they differ by exactly one letter. The goal is to find the shortest path from beginWord to endWord.
Start BFS from the beginWord, exploring transformations by changing each letter and checking if the new word exists in the set of available words. Polished implementation should consider preprocessing the word list for efficient lookup.
This C solution uses BFS to navigate from the beginWord to the endWord. It maintains a queue for exploration, marking words as visited to prevent revisiting them. The function oneLetterDifference checks if two words differ by exactly one letter, crucial for establishing edges in the BFS traversal.
C++
Java
Python
C#
JavaScript
Time Complexity: O(N * m^2), where N is the number of words in the list and m is the length of each word. Each word pair is compared in the worst case.
Space Complexity: O(N), where N is the number of nodes (words) we store in the queue.
| 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 |
G-29. Word Ladder - I | Shortest Paths • take U forward • 271,735 views views
Watch 9 more video solutions →Practice Word Ladder with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor