127. Word Ladder(M)
127. 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: 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.
For example, Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log","cog"]
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length . Note:
Return 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.
UPDATE (//):
The wordList parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes.
- Difficulty:Medium
- Total Accepted:129.6K
- Total Submissions:667.5K
- Contributor:LeetCode
class Solution {
public:
bool cmpwrd(string str1, string str2)
{
size_t strlen = str1.size();
int cnt = ;
for (int i = ; i < strlen; ++i)
{
if(str1[i] == str2[i]) ++cnt;
}
if(cnt == (strlen - )) return true;
return false;
} int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
if(find(wordList.begin(), wordList.end(), endWord) == wordList.end()) return ;
size_t wLlen = wordList.size();
vector<bool> visited(wLlen, false);
int sum = ;
queue<string> qs;
qs.push(beginWord);
qs.push("#"); // level flag while (!qs.empty())
{
string tmpstr = qs.front();
qs.pop();
if(tmpstr == "#")
{
if(!qs.empty())
{
qs.push(tmpstr);
tmpstr = qs.front();
qs.pop();
++sum;
}
else return ;
} if(tmpstr == endWord) return sum; //seek for all the possible next node
for (int j = ; j < wLlen; ++j)
{
if(!visited[j] && cmpwrd(tmpstr, wordList[j]))
{
if(tmpstr == endWord)
{
//cout << wordList[j] << "\n" << endWord << endl;
return (++sum);
}
//cout << wordList[j] << " ";
visited[j] = true;
qs.push(wordList[j]);
}
}
//cout << endl;
}
return sum;
}
};
12.21% Runtime: 806 ms 39 / 39 test cases passed
class Solution { //by kaishen2
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordDict) {
unordered_set<string> word_dict_;
for (auto &word : wordDict) word_dict_.insert(word);
if (word_dict_.find(endWord) == word_dict_.end()) return ;
else word_dict_.erase(endWord);
unordered_set<string> q1, q2, temp;
q1.insert(beginWord);
q2.insert(endWord);
int count = ;
while (!q1.empty() && !q2.empty()) {
++count;
if (q1.size() > q2.size()) swap(q1, q2);
temp.clear();
for (auto word_ : q1) {
for (int j = ; j < word_.size(); ++j) {
char hold = word_[j];
for (char i = 'a'; i <= 'z'; ++i) {
word_[j] = i;
if (q2.find(word_) != q2.end()) return count + ;
if (word_dict_.find(word_) != word_dict_.end()) {
word_dict_.erase(word_);
temp.insert(word_);
}
}
word_[j] = hold;
}
}
swap(q1, temp);
}
return ;
}
};
93.07% 35ms
127. Word Ladder(M)的更多相关文章
- 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
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
- [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 单词接龙(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...
- leetcode 127. Word Ladder ----- java
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- 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 (BFS / Graph)
https://leetcode.com/problems/word-ladder/ Given two words (beginWord and endWord), and a dictionary ...
- [leetcode]127. Word Ladder单词接龙
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
随机推荐
- WPF编程,通过Path类型制作沿路径运动的动画另一种方法。
原文:WPF编程,通过Path类型制作沿路径运动的动画另一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/article/d ...
- Spark(Python) 从内存中建立 RDD 的例子
Spark(Python) 从内存中建立 RDD 的例子: myData = ["Alice","Carlos","Frank"," ...
- arm学习之汇编跳转指令总结
目前所知道的跳转指令有 b,bl,bep,bne.他们共同点是都是以b开头,首先从字面上分析:b:是Branch,表示分支.bl:是Branch Link表示带连接的分支.bep:Branch ,Eq ...
- 利用OVS+FLOODLIGHT,为数据表添加VLAN_ID和MPLS
话不多说,直接上拓扑: 我这里是用主机h1 (10.0.0.1)ping 主机h2(10.0.0.2) 1.添加VLAN标签 v1: sudo ovs-ofctl add-flow m1-s1 in_ ...
- grunt源码解析:整体运行机制&grunt-cli源码解析
前端的童鞋对grunt应该不陌生,前面也陆陆续续的写了几篇grunt入门的文章.本篇文章会更进一步,对grunt的源码进行分析.文章大体内容内容如下: grunt整体设计概览 grunt-cli源码分 ...
- photoshop cs6安装过程中安装程序遇到错误:请重启计算机,解决办法
1.关闭防火墙和杀毒软件 2.删除注册表 依次展开HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager目录,找到其中的 ...
- web前端开发分享-css,js入门篇
学习没有捷径,但学习是有技巧与方法. 一,css入门篇: 推荐书籍:css哪些事儿,精通css. 理由:css那些事儿,他是一本介绍css基础类的书,是入门的经典读物. 系统的介绍了css的选 ...
- python3【基础】-赋值与深浅拷贝
一.Python的变量及其存储 在高级语言中,变量是对内存及其地址的抽象.对于python而言,python的一切变量都是对象,变量的存储,采用了引用语义的方式,存储的只是一个变量的值所在的内存地址, ...
- PAT甲题题解-1023. Have Fun with Numbers (20)-大数加法
和1024一样都是大数据的题,因为位数最多要20位,long long最多19位给一个num,求sum=num+num问sum包含的数字,是否是num的一个排列,即数字都一样,只是顺序不同罢了. #i ...
- Python网络数据采集
一.正则表达式 * 表匹配0次或者多次 a*b* + 表至少一次 [ ] 匹配任意一个 ( ) 辨识一个编组 {m,n} m或者n 次 [^] 匹配任意不在中括号里的字符 | ...