Word Ladder II 题解

题目来源:Word Ladder II

>

Given two words (start and end), and a dictionary, find all shortest
transformation sequence(s) from start to end, such that:
Only one letter can be changed at a time
Each intermediate word must exist in the dictionary
For example,
Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]
Return
[
    ["hit","hot","dot","dog","cog"],
    ["hit","hot","lot","log","cog"]
]
Note:
All words have the same length.
All words contain only lowercase alphabetic characters.

解题思路:

先BFS把邻接图构造出来,即比如与hit相差紧1个字符的单词有哪些~ 然后再dfs把所有结果搜索出来。有了word ladder的经验,这次就直接用26*length(word)去查找相邻的单词而不取dict搜索了。 其实到可以判断下 dict大小和 26*length(word)之间的关系决定选用哪种方法。

get了一种bfs的新技能,用一个queue,不用像上面那样两层之间交换。 一层一层之间加个特殊的节点表示层次之间的隔板(比如空指针啊,空串等)。 上面其他逻辑不变,bfs部分改变后的代码如下,也能AC。 针对本体的逻辑,注意最内层for循环(//!!!!!),不能直接加到q1中去,因为这样操作q1中可能含有重复的单词,会超时。

Last updated

Was this helpful?