Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:
Note:
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"] ]
Example 2:
Input: beginWord = "hit" endWord = "cog" wordList = ["hot","dot","dog","lot","log"] Output: [] Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.
分析:
接着上面 word-ladder 题,本题是要在上题的基础上把每一条最短路径打印出来。
这题的关键是如果大于最短路径,我先用了Python map来记录路径,即A->B,C->D这种,再通过dfs深度优先遍历来收集路径。
用dfs的方法能够在迭代结束时得到路径的个数,毕竟有多少条路径能够到达,就可以在此时生成多少条路径,并在回退的过程中把路径加进去。
代码如下:
class Solution:
def ladderLength(self, beginWord, endWord, wordList):
"""
:type beginWord: str
:type endWord: str
:type wordList: List[str]
:rtype: int
"""
wordSet = set(wordList)
if endWord in wordSet:
wordSet.remove(endWord)
else:
return []
endWordSet = set([endWord])
short_path = {}
is_find = True
while is_find:
curWordSet = set()
for ew in endWordSet:
short_path[ew] = []
for i in range(len(beginWord)):
for l in 'abcdefghijklmnopqrstuvwxyz':
c_w = ew[: i] + l + ew[i + 1:]
if c_w in wordSet:
short_path[ew].append(c_w)
curWordSet.add(c_w)
if c_w == beginWord:
short_path[ew] = [beginWord]
is_find = False
endWordSet = curWordSet
wordSet = wordSet - curWordSet
if is_find and len(endWordSet) == 0:
return []
if is_find is False:
short_path_lst = []
short_path_lst = self.printPaths(endWord, beginWord, short_path)
for rp in short_path_lst:
rp.append(endWord)
return short_path_lst
return []
def printPaths(self, beginWord, endWord, pathDict):
if beginWord not in pathDict:
return []
cur_path = pathDict[beginWord]
if endWord in cur_path: # 如果找到了路径,则返回该路径
return [[endWord]]
big_res = []
for p in cur_path: # 递归遍历map路径
res = self.printPaths(p, endWord, pathDict)
for rp in res:
rp.append(p)
big_res.extend(res)
return big_res