LeetCode "Word Ladder" - TRICKY

It is not as easy as I thought it to be, mostly because of time\space limitation. And actually that‘s the punch line of this problem

My intuition was DFS. I got several failing attemption - all TLE or MLE. Since what we are supposed to find is ‘shortest‘, BFS is definitely better than DFS: DFS exausts all paths but BFS doesn‘t! And that‘s not the end - think about the case that dictonary is huge, and here comes the 2nd important trick: we don‘t iterate dictionary to find children, instead we literally change a string by 26 chars one by one, and slot by slot.

Please mind these tricks - i spent hours in the snare but finally i got out of it and i think it deserves!

Check it here: http://zhaohongze.com/wordpress/2013/12/11/leetcode-word-ladder/
And also, another neat idea of Double BFS is here: http://yucoding.blogspot.com/2013/08/leetcode-question-127-word-ladder.html

LeetCode "Word Ladder" - TRICKY

时间: 2024-08-29 03:06:16

LeetCode "Word Ladder" - TRICKY的相关文章

[leetcode]Word Ladder II @ Python

[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://blog.csdn.net/doc_sgl/article/details/13341405   http://chaoren.is-programmer.com/ 题意:给定start单词,end单词,以及一个dict字典.要求找出start到end的所有最短路径,路径上的每个单词都要出现在dict

[python leetcode] Word Ladder II (very hard)[非常难,放弃]

Word Ladder II 描述 Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: • Only one letter can be changed at a time • Each intermediate word must exist in the dictionary For examp

leetcode Word Ladder II

和上题 Word Ladder I题目差不多,不过这里是要记录所有最段路径的可能. 不同点在于,我们不能再BFS每层的时候把相距一个字符的字符串在dict中删除,因为hot -> hit 的话其他的例如 jit -> hit 就是hit可以出现在两条路径里头.所以不能立马删除.但是我们发现我们可以删除的是我们遍历完的每层的字符串,我们用known来存遍历完的层,unknown来存没有遍历的层,那么每次求得下一层unknown的时候,就首先把known里面有的从dic中删除. 主要思路还是和上一

LeetCode Word Ladder 找单词变换梯

题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start到end这两个单词的变换序列.从start开始,每次可以变一个字母,且所变之后的单词必须在set中,最后要求变成end,问经过了多少个中间变换?注意要加多2次(start和end也要算),这是规定. 思路:广度搜索,以start为树根,一层一层扩展,直到找到end,返回数的深度即可.步骤是这样的,先

leetcode——Word Ladder

    好多天没写leetcode了,就是卡在这题上,然后各种偷懒....之前思路一直没理顺,想了两个晚上,就放弃了,后来就去看答案去了...可是答案没有注释,有点看不太懂,后来只好上网查了,有的说用到trie树,就去看了trie树是啥...反正那段时间状态不好,总不想看,偶尔还被跑男吸引注意力,总之各种偷懒,到了今天终于看完答案了,试着结合各方答案写了一下..调了2个小时,终于通过了...而且效率也不高...但不管怎样,终于做出来了...但是不知道为什么这么做能保证每个单词的步骤数得出来的是最

[LeetCode] Word Ladder

Well, this problem has a nice BFS structure. Let's see the example in the problem statement. start = "hit" end = "cog" dict = ["hot", "dot", "dog", "lot", "log"] Since only one letter c

leetcode——Word Ladder II

    改了3个晚上终于没有bug了...结果超时了...我已经想不出该怎么优化了....看了答案,是一层一层遍历,然后将每个单词的父节点push进一个vector里,我也是一层层,并且如果在下一层遇到上一层的某个单词,就记录单词被访问过,就不会再访问了...就是想不明白为啥用的时间要多很多...难道是因为我的代码不够简洁么?!!... class Solution { // 用广度搜索,建立由start开始的搜索树,子节点为父节点只改变一个字母在字典里能查到的所有的单词 // 每个子节点能记录

[LeetCode] Word Ladder II 之一

// 算法:DFS // 递归函数参数使用引用 // Time Limit Exceeded 1 class Solution { 2 public: 3 vector< vector<string> > findLadders( string start, string end, unordered_set<string > &dict) { 4 5 vector< vector< string> > ret; 6 vector<

leetcode Word Ladder 广搜

利用两个队列(或vector):curlevel和nextlvevl分别表示当前层的所有可能状态和转换到的下一层的所有可能状态.我们的目标是转换到end单词即可,深搜会超时.使用广搜,各层的可能结点同时向下进行,找到end即可return.当找完当前层的所有可能结点后,当前层也就空了,然后swap(当前层,下一层)循环向下搜索. 下面两个代码都是用这个方法,第一个超时,使用第二段代码,对于每个结点,只需找25*len种下一个可能结点是否存在,第一段代码需要在大数据里慢慢找. note:在一个字符