126. Word Ladder II

126. Word Ladder II

  • 方法1: BFS
  • 方法2: bidirectional BFS
  • 方法3: DFS

Given two words (beginWord and endWord), and a dictionary’s word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:

  1. 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:

  1. Return an empty list if there is no such transformation sequence.
  2. All words have the same length.
  3. All words contain only lowercase alphabetic characters.
  4. You may assume no duplicates in the word list.
  5. 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

你可能感兴趣的:(126. Word Ladder II)