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 ...
随机推荐
- 牛客OI周赛4-提高组-C-战争[并查集]
题意 一个长度为 \(n\) 的序列,每个权值互不相同,给出形如 \(l,r,p\) 的信息表示 \([l,r]\) 区间中最小的数是 \(p\) ,问第几个信息开始出现矛盾. \(n\leq 5 \ ...
- 大白话说Java泛型:入门、使用、原理
文章首发于[博客园-陈树义],点击跳转到原文<大白话说Java泛型:入门.使用.原理> 远在 JDK 1.4 版本的时候,那时候是没有泛型的概念的.当时 Java 程序员们写集合类的代码都 ...
- resource fork, Finder information, or similar detritus not allowed
1.关闭当前项目和Xcode 2.打开终端或者iterm cd ~/Library/Developer/Xcode/DerivedData/ 3. xattr -rc . 4.重新打开项目 5.如果不 ...
- kafka的简单理解
经典组合: Flume+Kafka+Storm+HDFS/HBase Flume:分布式采集 Kafka:分布式缓存 Kafka简介: 一种分布式的.基于发布/订阅的消息系统(Scala编写的) Ka ...
- Unity 2D相机公式换算(从其他博客上抄的)
2d camera, unit坐标,单位换算 2d游戏可以使用平行投影的camera,这种camera需要设置size (orthographicSize),size的含义为屏幕高度的一半,不过单位不 ...
- 《杜增强讲Unity之Tanks坦克大战》9-发射子弹时蓄力
9 发射子弹时蓄力 实现效果如下 image 按下开火键(坦克1为空格键)重置力为最小力,一直按着的时候蓄力,抬起的时候发射.如果按着的时候蓄力到最大,则自动发射,此时在抬起则不会重复发射. 首先 ...
- CoreDNS Plugins ---> hosts
需求 kubernetes集群外部有少量服务,kubernetes集群内部pod需要通过服务所在的主机的hostname访问服务. 解决方案 通过coredns的hosts插件配置kubernetes ...
- FUNMVP:5G技术对块链信任体系建设的影响
01 区块链现阶段应用在于概念证明 12月10日,工信部向三大运营商正式发放了5G系统实验频率运用允许,这让区块链从业者开端思索5G技术与区块链分别的可能性.在互联网的基础上依据区块链的特性完成价值的 ...
- pycharm 调试 scrapy
http://blog.csdn.net/shijichao2/article/details/61940931
- 《linux内核分析》chapter3读书笔记