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 ...
随机推荐
- powersheel远程连接方法操作
powersheel远程连接密码加密连接高级玩法 ConvertTo-SecureString 和 ConvertFrom-SecureString 命令都支持选项 -Key.在处理密码时通过使用 K ...
- Selenium 爬取全国水质周报Word
很久没写爬虫了 ,昨天有个学姐说需要爬取水质的一些数据,给了个网站( http://xxfb.hydroinfo.gov.cn/ssIndex.html?type=2&tdsourcetag= ...
- [UOJ#276][清华集训2016]汽水[分数规划+点分治]
题意 给定一棵 \(n\) 个点的树,给定 \(k\) ,求 \(|\frac{\sum w(路径长度)}{t(路径边数)}-k|\)的最小值. \(n\leq 5\times 10^5,k\leq ...
- Windows:查看IP地址,IP地址对应的机器名,占用的端口,以及占用该端口的应用程
Windows 服务器系列: Windows:查看IP地址,IP地址对应的机器名,占用的端口,以及占用该端口的应用程 Windows:使用Dos命令管理服务(Services) Windows:任务调 ...
- DokuWiki 使用
新建文件夹 修改url, 将新文件夹的名称赋值给url上的id, 如要建一个"DokuWiki"的文件夹,并在文件夹下新增一个"QuickStart"的页面,改 ...
- 简单模拟flume
NetCat方式: 远程访问的方式进行消息传递 配置一个Agent,主要配置三个组件: source, channel, sink 上图中为什么channel会带s,变成channels? 可以绑定多 ...
- Python提示信息表示内容
=此页面列出了PyLint 1.1.0支持的所有消息,按消息文本排序.还有一个按消息代码排序的所有代码列表. E0001,F0001,W0511(消息不同) E0103:循环中%r不正确W1501 ...
- arduino新入手体验:三个小实验
新入手体验:三个小实验 一:一个LED闪烁 控制要求:1个LED灯,每隔50ms闪烁一次 实物连接图: 控制代码: //2018.6/11 ;//定义数字接口10,对应 void setup() { ...
- Linux_01
要安装centos系统,就必须得有centos系统软件安装程序,可以通过浏览器访问centos官网http://www.centos.org,然后找到Downloads - > mirror ...
- openssl证书及配置
我的环境是:Linux+Apache+MySQL+PHP 1.下载openssl 及相关依赖 #yum install -y openssl 2.进入目录 /etc/pki/tls/certs #cd ...