[LeetCode][JavaScript]Word Ladder

Word Ladder

Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformation sequence from beginWord to endWord, such that:

  1. Only one letter can be changed at a time
  2. 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.

 https://leetcode.com/problems/word-ladder/

 

 


 

 

这题挂了无数次啊,一开始错把入参当做Array,其实是个Set,之后就一直超时...

bfs的思路是没错的,但首先不能构造图,复杂度O(n^2) 肯定不行。

然后尝试直接在Set里找,找到一个删除一个,也是超时。

最后只能换成了现在的方式。

这道题test case有特殊性,dict里的单词不会特别长,但是数据量很大。

最终改成在bfs队列中出队一个元素,一个个地用a-z替换每个字母,然后跟字典比较。

 1 /**
 2  * @param {string} beginWord
 3  * @param {string} endWord
 4  * @param {set<string>} wordDict
 5  * @return {number}
 6  */
 7 var ladderLength = function(beginWord, endWord, wordDict) {
 8     var queue = [];
 9     var i = 0;
10     queue.push(beginWord);
11     wordDict.delete(beginWord);
12     if(oneCharDiff(beginWord, endWord) && wordDict.has(endWord)){
13         return 2;
14     }else{
15         return bfs();
16     }
17 
18     function bfs(){
19         var depth = 1;
20         while(queue.length > 0){
21             depth++;
22             var count = queue.length;
23             while(count--){
24                 var curr = queue.shift();
25                 if(oneCharDiff(curr, endWord) && curr !== beginWord){
26                     return depth;
27                 }
28                 for(var i = 0; i < curr.length; i++){
29                     for(var j = 'a'.charCodeAt(); j <= 'z'.charCodeAt(); j++){
30                         var testMatch = curr;
31                         var ch = String.fromCharCode(j);
32                         if(testMatch[i] !== ch){
33                             testMatch = replaceChat(testMatch, i, ch);
34                         }
35                         if(wordDict.has(testMatch)){
36                             queue.push(testMatch);
37                             wordDict.delete(testMatch);
38                         }
39                     }
40                 }
41             }
42         }        
43         return 0;
44     }
45     function replaceChat(source, pos, newChar){  
46         var sFrontPart = source.substr(0, pos);  
47         var sTailPart = source.substr(pos + 1, source.length);  
48         return sFrontPart + newChar + sTailPart;  
49     }  
50     function oneCharDiff(a, b){
51         if(a.length !== b.length){
52             return false;
53         }
54         var count = 0;
55         for(var i = 0; i < a.length; i++){
56             if(a[i].toLowerCase() !== b[i].toLowerCase()){
57                 count++;
58             }
59             if(count >= 2){
60                 return false;
61             }
62         }
63         if(count === 1){
64             return true;
65         }else{
66             return false;
67         }
68     }
69 };

 

 

 

 

 

 

 

 
posted @ 2015-05-22 14:58  `Liok  阅读(876)  评论(0编辑  收藏  举报