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!

Input & Output

example_1.py โ€” Basic Transformation
$ Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
โ€บ Output: [["hit","hot","dot","dog","cog"],["hit","hot","lot","log","cog"]]
๐Ÿ’ก 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'.
example_2.py โ€” No Valid Path
$ Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
โ€บ Output: []
๐Ÿ’ก 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.
example_3.py โ€” Single Step
$ Input: beginWord = "a", endWord = "c", wordList = ["a","b","c"]
โ€บ Output: [["a","c"]]
๐Ÿ’ก 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
Word Transformation NetworkhitSTARThotdotlotdoglogcogENDiโ†’ohโ†’dhโ†’ltโ†’gtโ†’goโ†’coโ†’cBFS Distance Calculationhit: 0hot: 1dot: 2lot: 2dog: 3, log: 3cog: 4Shortest path length: 5 steps (hit โ†’ hot โ†’ dot/lot โ†’ dog/log โ†’ cog)Backtracking PhasePath 1: cog โ†’ dog โ†’ dot โ†’ hot โ†’ hit (distance decreases: 4โ†’3โ†’2โ†’1โ†’0)Path 2: cog โ†’ log โ†’ lot โ†’ hot โ†’ hit (distance decreases: 4โ†’3โ†’2โ†’1โ†’0)
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

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค beginWord.length โ‰ค 10
  • endWord.length == beginWord.length
  • 1 โ‰ค wordList.length โ‰ค 5000
  • wordList[i].length == beginWord.length
  • beginWord, endWord, and wordList[i] consist of lowercase English letters
  • beginWord โ‰  endWord
  • All the words in wordList are unique
Asked in
Amazon 45 Google 38 Meta 31 Microsoft 27 Apple 22
128.9K Views
Medium Frequency
~28 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen