
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.
- Start with "hit", can transform to "hot" (change 'i' to 'o').
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.
- Start with "hit", can transform to "hot".
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)
Editorial
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. |
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