0. 链接
1. 题目
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
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"]
]
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.
2. 思路1: BFS+DFS
- 基本思路是先通过BFS得到一棵抵达目标单词的最小深度树, 得到tree[node] = next_nodes
- 再通过DFS从beginWord开始向下遍历, 利用tree的辅助, 能抵达终点者收集合法路径, 然后回溯尝试其他路径
- 时间复杂度:
O(N+E)
,N
为words
个数,E
为words
中邻接边数 - 空间复杂度:
O(N)
,N
为words
个数
3. 代码
# coding:utf8
from typing import List
import collections
class Solution:
def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]:
# 先通过BFS得到抵达终点的最小深度树
word_set = set(wordList)
tree = collections.defaultdict(set)
nodes = {beginWord}
next_nodes = set()
found = False
while nodes and not found:
word_set -= nodes
for node in nodes:
for i in range(len(node)):
for ch in 'abcdefghijklmnopqrstuvwxyz':
next_word = node[:i] + ch + node[i + 1:]
if next_word in word_set:
if next_word == endWord:
found = True
else:
next_nodes.add(next_word)
tree[node].add(next_word)
nodes, next_nodes = next_nodes, set()
# 再通过DFS得到所有符合条件的答案
def dfs(word, result, results):
if word == endWord:
results.append(result.copy())
elif word in tree:
next_words = tree[word]
for each in next_words:
result.append(each)
dfs(each, result, results)
result.pop()
result = [beginWord]
results = list()
dfs(beginWord, result, results)
return results
def my_test(solution, beginWord, endWord, wordList):
print('input:\n\tbeginWord={}, endWord={}, wordList={}'.format(
beginWord, endWord, wordList
))
results = solution.findLadders(beginWord, endWord, wordList)
print('output:[')
for each in results:
print('\t\t{},'.format(each))
print('\t]')
print('=' * 100)
print('')
solution = Solution()
my_test(solution, 'hit', 'cog', ['hot', 'dot', 'dog', 'lot', 'log', 'cog'])
my_test(solution, "hot", "dog", ["hot", "cog", "dog", "tot", "hog", "hop", "pot", "dot"])
输出结果
input:
beginWord=hit, endWord=cog, wordList=['hot', 'dot', 'dog', 'lot', 'log', 'cog']
output:[
['hit', 'hot', 'lot', 'log', 'cog'],
['hit', 'hot', 'dot', 'dog', 'cog'],
]
====================================================================================================
input:
beginWord=hot, endWord=dog, wordList=['hot', 'cog', 'dog', 'tot', 'hog', 'hop', 'pot', 'dot']
output:[
['hot', 'hog', 'dog'],
['hot', 'dot', 'dog'],
]
====================================================================================================