Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start 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.

  DFS 小数据AC:

class Solution {
public:
bool check(const string & a, const string &b)
{
int num = ;
if(a.size() != b.size()) return false;
for(int i = ; i< a.size() ; i++)
{
if(a[i] != b[i])
num++;
}
return num == ;
}
void DFS(const string &start, const string &end, unordered_set<string> &dict, vector<bool> &flag, int nums){ if(start == end ){
res = res > nums ? nums : res;
return ;
}
int i;auto it = dict.begin();
for( i= ; it != dict.end(); it++,i++)
if(flag[i] == false && check(start,*it))
{
flag[i] = true;
DFS(*it,end,dict, flag, nums+);
flag[i] = false;
} }
int ladderLength(string start, string end, unordered_set<string> &dict) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
res = dict.size() + ;
vector<bool> flag(dict.size(), false);
DFS(start, end, dict, flag, );
if(res == dict.size() + ) return ;
return res + ;
}
private :
int res;
};

BFS: 过大数据

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.size() != end.size()) return ;
if(dict.size() == ) return ; queue<string> myqueue, myqueueT;
myqueue.push(start);
int depth = ; while(!myqueue.empty()){
depth++;
while(!myqueue.empty()){
string str = myqueue.front();
myqueue.pop();
for(int i = ; i < str.size() ; i++){
char temp = str[i] ;
for(char c = 'a'; c <= 'z' ;c++){
if(c == temp) continue;
str[i] = c;
if(str == end) return depth;
auto it = dict.find(str) ;
if(it != dict.end() ){
myqueueT.push(str);
dict.erase(it);
}
}
str[i] = temp;
}
}
myqueue.swap( myqueueT);
}
//don't find
return ;
}
};

LeetCode_Word Ladder的更多相关文章

  1. [LeetCode] Word Ladder 词语阶梯

    Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...

  2. [LeetCode] Word Ladder II 词语阶梯之二

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  3. LeetCode:Word Ladder I II

    其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...

  4. 【leetcode】Word Ladder

    Word Ladder Total Accepted: 24823 Total Submissions: 135014My Submissions Given two words (start and ...

  5. 【leetcode】Word Ladder II

      Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...

  6. 18. Word Ladder && Word Ladder II

    Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...

  7. [Leetcode][JAVA] Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  8. LeetCode127:Word Ladder II

    题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...

  9. 【LeetCode OJ】Word Ladder II

    Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...

随机推荐

  1. POJ 3525 Most Distant Point from the Sea

    http://poj.org/problem?id=3525 给出一个凸包,要求凸包内距离所有边的长度的最小值最大的是哪个 思路:二分答案,然后把凸包上的边移动这个距离,做半平面交看是否有解. #in ...

  2. NSIS检测操作系统x64还是x86的问题。

    想共同维护一个NSIS脚本文件的,不想搞两个版本的脚本文件了.开始想到了!if语句,没试过,不知道行不行得通.后来google了一下.可以用两个头文件搞定.参照下面链接 Reference: http ...

  3. logstash match

    [elk@zjtest7-frontend config]$ cat stdin04.conf input { stdin { } } filter { # drop sleep events gro ...

  4. ops

    consists several key projects separately stand-alone connected entities massive scalability massive ...

  5. (step5.1.6)hdu 1272(小希的迷宫——并查集)

    题目大意:输入一系列的点,判断这些点组成的图符不符合小希的思路(无环.连通) 解题思路: 1)如果两个节点的根节点相同,那么在这两个节点之间添加1条边以后,这个图肯定有环路. 2)孤立节点:被使用过& ...

  6. WPF - 为什么不能往Library的工程中添加WPF window

    项目中添加一个Library 工程,但是却无法加入WPF window, WPF customize control. 调查了一下,发现这一切都由于Library工程中没有:ProjectTypeGu ...

  7. [RxJS] Adding Conditional Logic with Filter

    Often you only want values to proceed through your stream if they meet certain criteria, just as if ...

  8. Samba通过ad域进行认证并限制空间大小《转载》

    本文实现了samba服务被访问的时候通过windows域服务器进行用户名和密码验证;认证通过的用户可以自动分配500M的共享空间;在用户通过windows域登陆系统的时候可以自动把这块空间映射成一块硬 ...

  9. JQuery或JavaScript获取网页的宽度、高等

    最近多次使用JQery或JavaScript获取网页的宽度或者高度,在网上搜索N久之后发现很多都是粘贴上去并没有详细的介绍,这里我将会对经常使用的一些获取页面宽高的属性,方法做详细的介绍,以便能够更加 ...

  10. 从反编译的角度去观察C#6.0

    1. 自动属性初始化 (Initializers for auto-properties) 1.1 C#6.0 之前的写法 public class FirstExperience { private ...