127. Word Ladder(M)

Posted peki10

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了127. Word Ladder(M)相关的知识,希望对你有一定的参考价值。

 

127. Word Ladder
Given two words (beginWord and endWord), and a dictionarys 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 5. Note: Return 0 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 (2017/1/20): 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.

 

技术分享
 1 class Solution {
 2 public:
 3     bool cmpwrd(string str1, string str2)
 4     {
 5         size_t strlen = str1.size();
 6         int cnt = 0;
 7         for (int i = 0; i < strlen; ++i)
 8         {
 9             if(str1[i] == str2[i]) ++cnt;
10         }
11         if(cnt == (strlen - 1)) return true;
12         return false;
13     }
14 
15     int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
16         if(find(wordList.begin(), wordList.end(), endWord) == wordList.end()) return 0;
17         size_t wLlen = wordList.size();
18         vector<bool> visited(wLlen, false);
19         int sum = 1;
20         queue<string> qs;
21         qs.push(beginWord);
22         qs.push("#"); // level flag
23 
24         while (!qs.empty())
25         {
26             string tmpstr = qs.front();
27             qs.pop();
28             if(tmpstr == "#")
29             {
30                 if(!qs.empty())
31                 {
32                     qs.push(tmpstr);
33                     tmpstr = qs.front();
34                     qs.pop();
35                     ++sum;
36                 }
37                 else return 0;
38             }
39 
40             if(tmpstr == endWord) return sum;
41 
42             //seek for all the possible next node
43             for (int j = 0; j < wLlen; ++j)
44             {
45                 if(!visited[j] && cmpwrd(tmpstr, wordList[j]))
46                 {
47                     if(tmpstr == endWord)
48                     {
49                         //cout << wordList[j] << "\n" << endWord << endl;
50                         return (++sum);
51                     }
52                     //cout << wordList[j] << " ";
53                     visited[j] = true;
54                     qs.push(wordList[j]);
55                 }
56             }
57             //cout << endl;
58         }
59         return sum;
60     }
61 };
View Code

12.21%  Runtime: 806 ms 39 / 39 test cases passed

 

技术分享
 1 class Solution {  //by kaishen2
 2 public:
 3     int ladderLength(string beginWord, string endWord, vector<string>& wordDict) {
 4         unordered_set<string> word_dict_;
 5         for (auto &word : wordDict) word_dict_.insert(word);
 6         if (word_dict_.find(endWord) == word_dict_.end()) return 0;
 7         else word_dict_.erase(endWord);
 8         unordered_set<string> q1, q2, temp;
 9         q1.insert(beginWord);
10         q2.insert(endWord);
11         int count = 0;
12         while (!q1.empty() && !q2.empty()) {
13             ++count;
14             if (q1.size() > q2.size()) swap(q1, q2);
15             temp.clear();
16             for (auto word_ : q1) {
17                 for (int j = 0; j < word_.size(); ++j) {
18                     char hold = word_[j];
19                     for (char i = a; i <= z; ++i) {
20                         word_[j] = i;
21                         if (q2.find(word_) != q2.end())  return count + 1;
22                         if (word_dict_.find(word_) != word_dict_.end()) {
23                             word_dict_.erase(word_);
24                             temp.insert(word_);
25                         }
26                     }
27                     word_[j] = hold;
28                 }
29             }
30             swap(q1, temp);
31         }
32         return 0;
33     }
34 };
View Code

93.07% 35ms

 


以上是关于127. Word Ladder(M)的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] 127 Word Ladder

127. Word Ladder

127. Word Ladder

算法: 词梯子127. Word Ladder

127. Word Ladder(js)

java 127. Word Ladder.java