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.

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

Visualization

Tap to expand
Word Ladder II - BFS Approach INPUT Word Transformation Graph hit hot dot lot dog log cog beginWord: "hit" endWord: "cog" wordList: [hot,dot,dog,lot,log,cog] ALGORITHM STEPS 1 Build Word Set Add all words to HashSet 2 BFS Level by Level Track all paths at each level Level 0: [hit] Level 1: [hot] Level 2: [dot, lot] Level 3: [dog, log] Level 4: [cog] FOUND! Remove visited words per level 3 Track Parents Map each word to predecessors 4 Backtrack Paths DFS from endWord to beginWord Time: O(N * L * 26) Space: O(N * L) for paths FINAL RESULT All Shortest Transformation Paths Path 1: hit --> hot --> dot --> dog --> cog Path 2: hit --> hot --> lot --> log --> cog Output: [ ["hit","hot","dot","dog","cog"], ["hit","hot","lot","log","cog"] ] 2 Paths OK Key Insight: BFS finds the shortest path length first. By processing level-by-level and removing visited words AFTER each level (not immediately), we allow multiple paths of the same length to be discovered. Track parent mappings to reconstruct ALL shortest paths via backtracking from endWord to beginWord. TutorialsPoint - Word Ladder II | BFS with Backtracking Approach
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