Tutorialspoint
Problem
Solution
Submissions

Word Ladder II

Certification: Advanced Level Accuracy: 0% Submissions: 0 Points: 15

Write a C program to find all shortest transformation sequences from beginWord to endWord. A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk where: Every adjacent pair of words differs by a single letter, every si for 1 <= i <= k is in wordList, and sk == endWord. Given two words, beginWord and endWord, and a dictionary wordList, return all the shortest transformation sequences from beginWord to endWord, or an empty list if no such sequence exists.

Example 1
  • Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
  • Output: [["hit","hot","dot","dog","cog"],["hit","hot","lot","log","cog"]]
  • Explanation:
    • Start with "hit", can transform to "hot" (change 'i' to 'o').
    • From "hot", can go to "dot" or "lot" (change 'h' to 'd' or 'l').
    • From "dot" -> "dog" -> "cog" or from "lot" -> "log" -> "cog".
    • Both paths have length 5, so both are shortest transformation sequences.
Example 2
  • Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
  • Output: []
  • Explanation:
    • Start with "hit", can transform to "hot".
    • From "hot", can reach "dot", "lot".
    • From "dot" can reach "dog", from "lot" can reach "log".
    • Neither "dog" nor "log" can reach "cog" since "cog" is not in wordList.
    • Therefore, no transformation sequence exists.
Constraints
  • 1 ≤ beginWord.length ≤ 5
  • endWord.length == beginWord.length
  • 1 ≤ wordList.length ≤ 500
  • wordList[i].length == beginWord.length
  • All words contain only lowercase English letters
  • Time Complexity: O(N * M * 26) where N is number of words, M is word length
  • Space Complexity: O(N * M)
AlgorithmsWalmartSwiggy
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use BFS to find shortest path length first
  • Build adjacency graph of words that differ by one character
  • Use backtracking to find all paths of shortest length
  • Keep track of visited words to avoid cycles
  • Store parent relationships during BFS for path reconstruction
  • Use level-order traversal to ensure shortest paths

Steps to solve by this approach:

 Step 1: Check if endWord exists in wordList, return empty result if not found
 Step 2: Use BFS to find the shortest transformation length from beginWord to endWord
 Step 3: Create a queue and add beginWord as starting point with level 1
 Step 4: For each word in queue, find all adjacent words (differ by one character) in wordList
 Step 5: Continue BFS until endWord is reached to get shortest path length
 Step 6: Use backtracking to find all possible paths of the shortest length
 Step 7: Return all valid transformation sequences that achieve the shortest length

Submitted Code :