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 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. 这个题的思路很明显也是BFS, 因为我们是一层一层的scan, 所以一旦找到endword, 那么肯定目前的distance是最小的. 我们怎么样BFS呢, 把目前的word的每一位分别用26位小写字母去代替,
得到一个new_word, 看new_word是否在wordlist里面 并且没有visited过, 如果是,append 进入queue, 并且将其标志为visited. 这里有个improve的是不用代替26个字母, 而用
chars = set(c for word in wordlist for c in word), 就是将wordlist里面所有的字母放到一个list里面, 只用试这些字母就行了, 加快一点进程. edge case的话就是如果endword
不在wordlist里面. 1. Constraints
1) beginWord and endWord is not the same
  2) all words are lower cases # 如果我们chars用improve的方法,也就是思路里面的方式, 就算有大写字母也无所谓.
  3) no duplicates in wordlist
  4) wordlist can be empty
  5) one letter change in one time
  6) all words has the same length
  7) beginWord and endWord not empty
  8) edge case, endword has to be in wordlist, otherwise return 0 2. Ideas BFS: T: O(m*n) S: O(m*n) m: length of wordlist, n length of each word 1) edge case, endword not in set(wordlist) => 0
2) queue(init: (beginword, 1)), visited (init: set())
3) while queue: word, dis = queue.popleft(), if word == endword, return dis
4) else:将word的每一位分别用chars里面的字母代替, 然后看是否在wordlist里面并且没有被visited过, 如果是, append进入queue, 并且tag为visited
5) end loop, return 0 3. code
 class Solution:
def wordLadder(self, beginWord, WordList, endWord):
len_word, w_list = len(beginWord), set(WordList) # set(WordList) 非常重要,否者的话time limit
if endWord not in w_list: return 0 # edge case
queue, visited = collections.deque([(beginWord, 1)]), set()
chars = set(c for word in w_list for c in word)
while queue:
word, dis = queue.popleft()
if word == endWord: return dis
else:
for i in range(len_word):
for c in chars:
new_word = word[:i] + c + word[i+1:]
if new_word in w_list and new_word not in visited:
queue.append((new_word, dis+1))
visited.add(new_word)
return 0

4. test cases:

1) wordList is empty or all wordlist not include endWord.  => 0

2)

Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"] Output: 5

[LeetCode] 127. Word Ladder _Medium tag: BFS的更多相关文章

  1. [LeetCode] 127. Word Ladder 单词阶梯

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  2. leetcode 127. Word Ladder、126. Word Ladder II

    127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...

  3. Leetcode#127 Word Ladder

    原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...

  4. LeetCode 127. Word Ladder 单词接龙(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...

  5. leetcode@ [127] Word Ladder (BFS / Graph)

    https://leetcode.com/problems/word-ladder/ Given two words (beginWord and endWord), and a dictionary ...

  6. leetcode 127. Word Ladder ----- java

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  7. [leetcode]127. Word Ladder单词接龙

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  8. Java for LeetCode 127 Word Ladder

    Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...

  9. [LeetCode] 126. Word Ladder II 词语阶梯 II

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

随机推荐

  1. js阻止浏览器的默认行为以及停止事件冒泡(用JQuery实现回车提交,兼容IE、FF浏览器) 转

    1.阻止浏览器的默认行为 function stopDefault(e) { //如果提供了事件对象,则这是一个非IE浏览器 if(e && e.preventDefault) { / ...

  2. MySQL DROP 大表时的注意事项

    对于表的删除,因为InnoDB引擎会在table cache层面维护一个全局独占锁一直到DROP TABLE完成为止,这样,对于表的其他操作会被HANG住.对于较大的表来说,DROP TABLE操作可 ...

  3. Node.j中path模块对路径的操作

    一.path模块 https://nodejs.org/docs/latest/api/path.html#path_path_join_paths 1.join方法 ==> 该方法将多个参数值 ...

  4. 基本类型算法题目学习(EPI)

    1.关于奇偶校验的方法中,如何快速的求取一个64-bit的数字的奇偶校验位.(如果1的位数为奇数,则奇偶校验位为1,如果1的位数为偶数,则奇偶校验位为0) a.暴力枚举法采用一位一位进行计算,一位一位 ...

  5. 如何搭建web服务器 使用Nginx搭建反向代理服务器 .

    引言:最近公司有台服务器遭受DDOS攻击,流量在70M以上,由于服务器硬件配置较高所以不需要DDOS硬件防火墙.但我们要知道,IDC机房是肯定不允许这种流量一直处于这么高的,因为没法具体知道后面陆续攻 ...

  6. Mecanim高级主题:Mecanim Blend Tree应用、Blend Tree 选项、复合Blend Tree

    一.Blend Tree介绍及应用 一个游戏动画的基本任务就是将两个或多个相似的动作混合.也许最广为人知的例子就是依照任务行动的速度将行走和跑动动画混合起来了.另一个例子就是角色在跑动中向左或向右转身 ...

  7. Android开发小技巧之根据position判断ListView是否在显示

    使用ListView的时候,会有判断指定项是否正在显示的需求,其实很简单,代码如下: private boolean isShowing(int position) { int showViewCou ...

  8. thinkCMF----自定义配置调用

    有些时候,需要在后台给网站一些其他的配置: 这个配置,一般都是通过修改代码实现的,ThinkCMF本身没有这个配置: 找到site.html 增加一个Group就可以: 在配置里面做相应的配置就可以:

  9. thinkphp---模糊查询详解

    最近做项目,在做搜索模块的时候,模糊查询肯定少不了. 今天就详细的看一下模糊查询: $where['title'] = array('like','%'.$words.'%'); $where['ti ...

  10. R生存分析AFT

    γ = 1/scale =1/0.902 α = exp(−(Intercept)γ)=exp(-(7.111)*γ) > library(survival) > myfit=survre ...