Question
Given two words (beginWord and endWord), and a dictionary’s word list, find the length of shortest transformation sequence 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 0 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: 5
Explanation: As one shortest transformation is “hit” -> “hot” -> “dot” -> “dog” -> “cog”,
return its length 5.
Example 2:
Input:
beginWord = “hit”
endWord = “cog”
wordList = [“hot”,”dot”,”dog”,”lot”,”log”]
Output: 0
Explanation: The endWord “cog” is not in wordList, therefore no possible transformation.
Solution
采用广度优先算法(BFS)
- 增加附加类来标识遍历节点,包含当前节点的单词跟对应的树的层次
- 不采用辅助函数,因为采用辅佐函数就时间复杂度是NL(N是wordList的长度,L是单词的长度),题目有个特性是都是a-z的字符,说明只需遍历L*26次就可以穷举邻居,这个时间复杂度上要少很多
- 时间复杂度 O(n*26^l) 空间复杂度 O(n)
Runtime: 53 ms, faster than 83.76% of Java online submissions for Word Ladder.
Memory Usage: 39.5 MB, less than 84.04% of Java online submissions for Word Ladder.
1 | class Solution { |
采用双向广度优先算法(Bi-BFS)
- 跟前面解题方法类似,但是区别是分别从起点跟终点来触发找邻居集合(子节点集合)
- 在查找过程中,需要交替找下集子节点集合,这里通过判断上下两个集合集的大小
- 最后发现他们会在中间层相遇,形成一个菱形的树状接口
- 时间复杂度 O(n*26^l/2) 空间复杂度 O(n)
Runtime: 14 ms, faster than 98.75% of Java online submissions for Word Ladder.
Memory Usage: 39.8 MB, less than 80.62% of Java online submissions for Word Ladder.
1 | class Solution { |