[LeetCode] 127. Word Ladder _Medium tag: BFS
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.
- 这个题的思路很明显也是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的更多相关文章
- [LeetCode] 127. Word Ladder 单词阶梯
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- leetcode 127. Word Ladder、126. Word Ladder II
127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...
- Leetcode#127 Word Ladder
原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...
- LeetCode 127. Word Ladder 单词接龙(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...
- leetcode@ [127] Word Ladder (BFS / Graph)
https://leetcode.com/problems/word-ladder/ Given two words (beginWord and endWord), and a dictionary ...
- leetcode 127. Word Ladder ----- java
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- [leetcode]127. Word Ladder单词接龙
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- Java for LeetCode 127 Word Ladder
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- [LeetCode] 126. Word Ladder II 词语阶梯 II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
随机推荐
- c++ 用new创建二维数组~创建指针数组【转】
#include <iostream> using namespace std; void main() { //用new创建一个二维数组,有两种方法,是等价的 //一: ] = ][]; ...
- angularjs结合html5的拖拽行为
闲聊: 小颖公司的项目之前要实现一个将左侧树中当前拖拽的内容,动态添加到右侧树种,虽然这个模块当时没有分给小颖,是同事完成的(小颖也不会嘻嘻),后来看了下他写代码,小颖自己写了个小demo.就当做个笔 ...
- Oracle中V$SESSION等各表的字段解释,Oracle官方解释
一.常用的视图 1.会话相关视图 View Description V$PROCESS Contains information about the currently active processe ...
- 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十八:SDRAM模块① — 单字读写
实验十八:SDRAM模块① — 单字读写 笔者与SDRAM有段不短的孽缘,它作为冤魂日夜不断纠缠笔者.笔者尝试过许多方法将其退散,不过屡试屡败的笔者,最终心情像橘子一样橙.<整合篇>之际, ...
- github命令行下载项目源码
一.git clone [URL] 下载指定ur的源码 $ git clone https://github.com/jquery/jquery 二.指定参数, -b是分支, --depth 1 最新 ...
- 以太网端口二种链路类型:Access 和Trunk
Access 类型的端口:只能属于1 个VLAN,一般用于连接计算机的端口: Trunk 类型的端口:可以允许多个VLAN 通过,可以接收和发送多个VLAN 的报文,一般用于交换机之间连接的端口 ...
- 提高VS2010运行速度的技巧
任务管理器,CPU和内存都不高,为何?原因就是VS2010不停地读硬盘导致的; 写代码2/3的时间都耗在卡上了,太难受了; 研究发现,VS2010如果你装了VC等语言,那么它就会自动装SQL Serv ...
- 简单ORM工具的设计和编写,自己项目中曾经用过的
http://www.cnblogs.com/szp1118/archive/2011/03/30/ORM.html 在之前的一个项目中自己编写了一个简单的ORM小工具,这次重新整理和重构了一下代码, ...
- ios中的coredata的使用
Core Data数据持久化是对SQLite的一个升级,它是iOS集成的,在说Core Data之前,我们先说说在CoreData中使用的几个类. (1)NSManagedObjectModel(被管 ...
- C++ 术语(C++ Primer)
argument(实参):传递给被调用函数的值.block(块):花括号括起来的语句序列.buffer(缓冲区):一段用来存放数据的存储区域.IO 设备常存储输入(或输出)到缓冲区,并独立于程序动作对 ...