126. Word Ladder II
Given two words (beginWord and endWord), and a dictionary’s word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:
- Only one letter can be changed at a time
2。 Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
Note:
- Return an empty list if there is no such transformation sequence.
- All words have the same length.
- All words contain only lowercase alphabetic characters.
- You may assume no duplicates in the word list.
- You may assume beginWord and endWord are non-empty and are not the same.
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"]
]
方法1: BFS
花花酱: https://www.youtube.com/watch?v=PblfQrdWXQ4
grandyang:http://www.cnblogs.com/grandyang/p/4548184.html
方法2: bidirectional BFS
花花酱: https://www.youtube.com/watch?v=PblfQrdWXQ4
方法3: DFS
?王:https://www.youtube.com/watch?v=lmypbtgdpuQ
由于求的是所有路径,bfs其实不太适合,可以考虑dfs,但是需要优化防止overflow