|
| 1 | +# 127. Word Ladder |
| 2 | + |
| 3 | +**<font color=red>难度:Medium<font>** |
| 4 | + |
| 5 | +## 刷题内容 |
| 6 | + |
| 7 | +> 原题连接 |
| 8 | +
|
| 9 | +* https://leetcode.com/problems/word-ladder/ |
| 10 | + |
| 11 | +> 内容描述 |
| 12 | +
|
| 13 | +``` |
| 14 | +Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that: |
| 15 | +
|
| 16 | +Only one letter can be changed at a time. |
| 17 | +Each transformed word must exist in the word list. Note that beginWord is not a transformed word. |
| 18 | +Note: |
| 19 | +
|
| 20 | +Return 0 if there is no such transformation sequence. |
| 21 | +All words have the same length. |
| 22 | +All words contain only lowercase alphabetic characters. |
| 23 | +You may assume no duplicates in the word list. |
| 24 | +You may assume beginWord and endWord are non-empty and are not the same. |
| 25 | +Example 1: |
| 26 | +
|
| 27 | +Input: |
| 28 | +beginWord = "hit", |
| 29 | +endWord = "cog", |
| 30 | +wordList = ["hot","dot","dog","lot","log","cog"] |
| 31 | +
|
| 32 | +Output: 5 |
| 33 | +
|
| 34 | +Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog", |
| 35 | +return its length 5. |
| 36 | +Example 2: |
| 37 | +
|
| 38 | +Input: |
| 39 | +beginWord = "hit" |
| 40 | +endWord = "cog" |
| 41 | +wordList = ["hot","dot","dog","lot","log"] |
| 42 | +
|
| 43 | +Output: 0 |
| 44 | +
|
| 45 | +Explanation: The endWord "cog" is not in wordList, therefore no possible transformation. |
| 46 | +
|
| 47 | +``` |
| 48 | +> 思路1 |
| 49 | +
|
| 50 | +******- 时间复杂度: O(EV+V^3)******- 空间复杂度: O(n^2)****** |
| 51 | + |
| 52 | +这是一道不错的题,第一种用BFS解,首先我们构造一个图,用二维数组储存,然后BFS一层一层搜索。上面E是边的数量,v是节点的数量 |
| 53 | + |
| 54 | +```cpp |
| 55 | +class Solution { |
| 56 | +public: |
| 57 | + int ladderLength(string beginWord, string endWord, vector<string>& wordList) { |
| 58 | + wordList.push_back(beginWord); |
| 59 | + int len = wordList[0].length(),length = wordList.size(); |
| 60 | + queue<int> q; |
| 61 | + vector<int> d[length]; |
| 62 | + int min_l[length]; |
| 63 | + for(int i = 0;i < length;++i) |
| 64 | + min_l[i] = INT_MAX; |
| 65 | + for(int i = 0;i < length;++i) |
| 66 | + for(int j = i + 1;j <length;++j) |
| 67 | + { |
| 68 | + int count1 = 0; |
| 69 | + for(int t = 0;t < len;++t) |
| 70 | + if(wordList[i][t] != wordList[j][t]) |
| 71 | + count1++; |
| 72 | + if(count1 == 1) |
| 73 | + { |
| 74 | + d[i].push_back(j); |
| 75 | + d[j].push_back(i); |
| 76 | + } |
| 77 | + } |
| 78 | + int exit = -1; |
| 79 | + for(int i = 0;i < length;++i) |
| 80 | + if(wordList[i] == endWord) |
| 81 | + { |
| 82 | + exit = i; |
| 83 | + min_l[i] = 0; |
| 84 | + q.push(i); |
| 85 | + } |
| 86 | + if(exit == -1) |
| 87 | + return 0; |
| 88 | + while(q.size()) |
| 89 | + { |
| 90 | + int t = q.front(); |
| 91 | + for(int i = 0;i < d[t].size();++i) |
| 92 | + if(min_l[d[t][i]] > min_l[t] + 1) |
| 93 | + { |
| 94 | + q.push(d[t][i]); |
| 95 | + min_l[d[t][i]] = min_l[t] + 1; |
| 96 | + if(d[t][i] == length - 1) |
| 97 | + return min_l[length - 1] + 1; |
| 98 | + } |
| 99 | + q.pop(); |
| 100 | + } |
| 101 | + return 0; |
| 102 | + } |
| 103 | +}; |
| 104 | +``` |
| 105 | +
|
| 106 | +
|
| 107 | +> 思路2 |
| 108 | +
|
| 109 | +******- 时间复杂度: O(EV*len)******- 空间复杂度: O(V)****** |
| 110 | +
|
| 111 | +上面的方法效率不高,因此可以改用用unordered_set储存所有的字符串,然后每次只改动一个字符,在set中查找这个单词是否存在即可。上面的len是每个单词的长度。 |
| 112 | +
|
| 113 | +```cpp |
| 114 | +class Solution { |
| 115 | +public: |
| 116 | + int ladderLength(string beginWord, string endWord, vector<string>& wordList) { |
| 117 | + wordList.push_back(beginWord); |
| 118 | + int len = wordList[0].length(),length = wordList.size(); |
| 119 | + queue<string> q; |
| 120 | + unordered_set<string> s(wordList.begin(),wordList.end()); |
| 121 | + int min_l[length]; |
| 122 | + auto pos = s.find(endWord); |
| 123 | + if(pos == s.end()) |
| 124 | + return 0; |
| 125 | + q.push(*pos); |
| 126 | + int level = 1; |
| 127 | + string count1 = endWord; |
| 128 | + while(q.size()) |
| 129 | + { |
| 130 | + string temp1 = q.front(); |
| 131 | + for(int j = 0;j < temp1.size();++j) |
| 132 | + { |
| 133 | + string temp = temp1; |
| 134 | + for(int i = 'a';i <= 'z';++i) |
| 135 | + { |
| 136 | + temp[j] = i; |
| 137 | + if(temp != temp1 && s.find(temp) != s.end()) |
| 138 | + { |
| 139 | + q.push(temp); |
| 140 | + if(temp == beginWord) |
| 141 | + return level + 1; |
| 142 | + s.erase(temp); |
| 143 | + } |
| 144 | + temp = temp1; |
| 145 | + } |
| 146 | + } |
| 147 | + if(count1 == temp1) |
| 148 | + { |
| 149 | + count1 = q.back(); |
| 150 | + level++; |
| 151 | + } |
| 152 | + s.erase(temp1); |
| 153 | + q.pop(); |
| 154 | + } |
| 155 | + return 0; |
| 156 | + } |
| 157 | +}; |
| 158 | +``` |
0 commit comments