Practice/Bloomberg/Leetcode 126. Word Ladder II
CodingMust
You are given a starting word, an ending word, and a dictionary of valid words. Each word has the same length. In one transformation step, you can change exactly one letter of a word to create a new word that must exist in the dictionary.
Find all shortest possible transformation sequences that convert the starting word into the ending word. Each sequence should contain only valid words from the dictionary (plus the starting word), and each consecutive pair of words should differ by exactly one letter.
If no transformation sequence exists, return an empty list. If multiple shortest sequences exist, return all of them.
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: Two shortest transformation sequences exist:
Example 2:
` Input: beginWord = "hit" endWord = "cog" wordList = ["hot","dot","dog","lot","log"]
Output: []
Explanation: The endWord "cog" is not in the dictionary, so no valid transformation exists. `
Example 3:
` Input: beginWord = "red" endWord = "tax" wordList = ["ted","tex","red","tax","tad","den","rex","pee"]
Output: [["red","ted","tad","tax"],["red","ted","tex","tax"],["red","rex","tex","tax"]]
Explanation: Three different shortest paths exist, all with the same length. `