[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 ...
随机推荐
- Mysql错误:Duplicate entry '127' for key 'PRIMARY'的解决方法
有时候真是挺幸运,正当我自以为是地认为掌握了某个知识点的时候,现实就会马上出现另外一个问题,让我知道之前的认知是不全面的. 正如我上篇博文中所述,如果一个自增字段达到了上限,而且继续向里面插入数据的话 ...
- liunx trac 插件使用之DateFieldPlugin
插件GanttCalendarPlugin安装完以后,有一个问题,就是在选择起始与结束时间的时候,为了方便有datepicker功能,如图 需要用到插件DateFieldPlugin,官网链接http ...
- C++中的异常安全性【转】
原文写的非常好,来自这里 一个函数如果说是“异常安全”的,必须同时满足以下两个条件:1.不泄漏任何资源:2.不允许破坏数据. 我们先通过两个反面的例子开始. 第一个是造成资源泄漏的例子.一个类Type ...
- 【WEB前端系列之CSS】CSS3动画之Animation
前言 动画使用示例https://github.com/AndyFlower/web-front/tree/master/css3/loading 学习CSS3中Animation之前先来看一个动画特 ...
- 流程图 --- BPMN规范简介
BPMN 目前 是2.0规范 http://www.bpmn.org/ BPMN Quick Guide http://blog.csdn.net/flygoa/article/details/5 ...
- Redis学习笔记--Redis配置文件Sentinel.conf参数配置详解
redis-sentinel.conf配置项说明如下: 1.port 26379 sentinel监听端口,默认是26379,可以修改. 2.sentinel monitor <master-n ...
- eclipse安装maven时候如果conf文件夹中有setting文件则会以这个文件为主,如果自己设置了user的配置文件则会无效
eclipse安装maven时候如果conf文件夹中有setting文件则会以这个文件为主,如果自己设置了user的配置文件则会无效
- Unity3D Shader图像扭曲过场效果
把脚本挂在摄像机上 using UnityEngine; using System.Collections; [RequireComponent(typeof(Camera))] public cla ...
- MFC创建好的对话框如何移植到新程序中
1.用文本文件打开需要移植对话框工程中的rc文件 2.在RC文件夹中找到需要移植的对话框内容,然后拷贝到新的工程的rc文件中 3.在原有工程的rsource.h中所有和这个对话框有关的ID都拷贝到新的 ...
- 【POJ2888】Magic Bracelet Burnside引理+欧拉函数+矩阵乘法
[POJ2888]Magic Bracelet 题意:一个长度为n的项链,有m种颜色的珠子,有k个限制(a,b)表示颜色为a的珠子和颜色为b的珠子不能相邻,求用m种珠子能串成的项链有多少种.如果一个项 ...