Word Ladder

来源:互联网 发布:淘宝助理没有宝贝管理 编辑:程序博客网 时间:2024/06/02 02:22

Given two words (beginWord and endWord), and a dictionary, 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 intermediate word must exist in the dictionary

For example,

Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]

As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.

Note:

  • Return 0 if there is no such transformation sequence.
  • All words have the same length.
  • All words contain only lowercase alphabetic characters.


思路1:bfs,遍历str的每个字母,进行修改,如果是在字典中,且之前并未出现过的字符串(防止出现循环),则将其放入到队列中

思路2:根据思路1,有点图的感觉。。。最短路径?

思路3:dfs,做肯定可以做,但其实没必要,因为并不是一个(现在发现其实dfs是有问题的,因为路径长短的问题
0 0