word ladder
来源:互联网 发布:苹果app加密软件 编辑:程序博客网 时间:2024/05/15 10:39
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence fromstart to end, such that:
- Only one letter can be changed at a time
- Each intermediate word must exist in the dictionary
For example,
Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog"
,
return its length 5
.
Note:
- Return 0 if there is no such transformation sequence.
- All words have the same length.
- All words contain only lowercase alphabetic characters.
class Solution {public: int ladderLength(string start, string end, unordered_set<string> &dict) { // Start typing your C/C++ solution below // DO NOT write int main() function if(start.compare(end) == 0) return 0;//int count = 0;unordered_map<int, string> mp;unordered_map<string, int> inverse;unordered_set<string> vis;queue<string> q;int count = 1;int deep = 1;int len = start.length();q.push(start);while(count > 0){while(count > 0){string temp = q.front();if(temp.compare(end) == 0)return deep;q.pop();for(int i = 0; i < len; i++){char x = temp[i];for(char c = 'a'; c <= 'z'; c++){temp[i] = c;if(dict.count(temp) && !vis.count(temp)){vis.insert(temp);q.push(temp);}}temp[i] = x;}count--;}count = q.size();deep ++;}return 0; }};
- Word Ladder
- Word Ladder
- word ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- EverNote
- javascript相关文章
- [转]FileZilla Server 设置
- hdu 4519 郑厂长系列故事——体检(贪心)
- java产生随机数方法比较
- word ladder
- C++拷贝构造函数(深拷贝,浅拷贝)
- Android菜鸟笔记-实现一键重启和关机
- 求助:如何组建企业局域网
- KMP匹配算法
- ALM11的REST API
- console控制台程序隐藏窗口
- 域名筛选网站
- 基于openSER 的 sip register 注册过程