Imagine you're a linguist working with word transformations! You need to find all the shortest possible paths to transform one word into another by changing exactly one letter at a time.
Given a beginWord, an endWord, and a dictionary of valid words (wordList), your task is to return all the shortest transformation sequences from beginWord to endWord.
Rules for transformation:
Each step changes exactly one letter
Every intermediate word must exist in the wordList
The beginWord doesn't need to be in wordList
All words have the same length and contain only lowercase letters
Example: Transform "hit" โ "cog"
Possible path: ["hit", "hot", "dot", "dog", "cog"]
Another path: ["hit", "hot", "lot", "log", "cog"]
Return an empty list if no transformation sequence exists. If multiple shortest paths exist, return all of them!
๐ก Note:There are 2 shortest transformation sequences of length 5. Both start with 'hit'โ'hot', then diverge: one goes through 'dot'โ'dog' and the other through 'lot'โ'log', both ending at 'cog'.
๐ก Note:The endWord 'cog' is not in the wordList, so no transformation sequence is possible. We cannot transform to a word that doesn't exist in our dictionary.
๐ก Note:Direct transformation from 'a' to 'c' in one step (changing one letter). This is the shortest possible path with length 2.
Visualization
Tap to expand
Understanding the Visualization
1
Build Word Graph
Create connections between words that differ by exactly one letter
2
BFS for Distances
Use BFS to find shortest distance from start word to all reachable words
3
Backtrack Paths
From end word, follow only edges leading to words closer to start
4
Collect Results
All valid backtracking paths give us the shortest transformation sequences
Key Takeaway
๐ฏ Key Insight: Use BFS to find shortest distances first, then backtrack following only edges that decrease distance by 1. This ensures we find ALL shortest paths efficiently without exploring suboptimal routes.
Time & Space Complexity
Time Complexity
โฑ๏ธ
O(N * 26 * L + B^D)
N words ร 26 letters ร L length for BFS, plus B^D for backtracking where B is branching factor and D is depth
n
2n
โ Linear Growth
Space Complexity
O(N * L)
Space for neighbor graph, distance map, and recursion stack
beginWord, endWord, and wordList[i] consist of lowercase English letters
beginWord โ endWord
All the words in wordList are unique
Asked in
A
Amazon 45G
Google 38f
Meta 31โ
Microsoft 27๐
Apple 22
Word Ladder II โ Solution
The optimal approach uses BFS + Backtracking: First, run BFS from beginWord to find the shortest distance to all reachable words. Then, use backtracking from endWord, only following edges that lead to words with distance = current_distance - 1. This guarantees we only construct shortest paths while finding all possible ones. Time complexity: O(N ร 26 ร L + B^D) where N is word count, L is word length, B is branching factor, and D is shortest path depth.
Common Approaches
Approach
Time
Space
Notes
โ
BFS + Backtracking (Standard Approach)
O(N * 26 * L + B^D)
O(N * L)
Use BFS to find shortest distances, then backtrack to reconstruct all shortest paths