Word Ladder

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:

  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:

  • 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

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:

Note

这题其实是BFS + Graph,但也不容易了。本质是使用BFS找抽象/隐式图的最短路径

把各个图节点想象成每个单词,相邻节点的edit distance都是1,把起点和终点也加进去

BFS遍历搜索字典,由于需要返回层数,需要分层,三层循环,最内层遍历的是one edit distance的包含在字典的所有单词

搜索字典的时候需要对每个单词的字母通过26个字母变换,否则如果字典单词很多会超时

I think, that time complexity is O(N*L^2), where N is size of the dictionary and M is length of the word

  • To generate neighbors - O(26 * L)

  • To check if the word exists in dict - O(L). This is a reason why it is better to put all words to theSet. Note, that the original version of this problem usesList <String> wordList. It is a bit confusing since author changed the signature toSet<String> dict.

  • To generate a tree and traverse the tree via BFS - O(N)

So, the result is O(26 * L * L * N) -> O(L^2 * N)

ps: hash_map的时间是O(key.size()) 因为key变成hash code即为index

优化可以用双向BFS,起点终点相遇

Code

Last updated