|
2 | 2 | // Author : henrytine
|
3 | 3 | // Date : 2020-08-11
|
4 | 4 |
|
5 |
| -/***************************************************************************************************** |
| 5 | +/***************************************************************************************************** |
6 | 6 | *
|
7 |
| - * Given two words (beginWord and endWord), and a dictionary's word list, find all shortest |
| 7 | + * Given two words (beginWord and endWord), and a dictionary's word list, find all shortest |
8 | 8 | * transformation sequence(s) from beginWord to endWord, such that:
|
9 |
| - * |
| 9 | + * |
10 | 10 | * Only one letter can be changed at a time
|
11 |
| - * Each transformed word must exist in the word list. Note that beginWord is not a transformed |
| 11 | + * Each transformed word must exist in the word list. Note that beginWord is not a transformed |
12 | 12 | * word.
|
13 |
| - * |
| 13 | + * |
14 | 14 | * Note:
|
15 |
| - * |
| 15 | + * |
16 | 16 | * Return an empty list if there is no such transformation sequence.
|
17 | 17 | * All words have the same length.
|
18 | 18 | * All words contain only lowercase alphabetic characters.
|
19 | 19 | * You may assume no duplicates in the word list.
|
20 | 20 | * You may assume beginWord and endWord are non-empty and are not the same.
|
21 |
| - * |
| 21 | + * |
22 | 22 | * Example 1:
|
23 |
| - * |
| 23 | + * |
24 | 24 | * Input:
|
25 | 25 | * beginWord = "hit",
|
26 | 26 | * endWord = "cog",
|
27 | 27 | * wordList = ["hot","dot","dog","lot","log","cog"]
|
28 |
| - * |
| 28 | + * |
29 | 29 | * Output:
|
30 | 30 | * [
|
31 | 31 | * ["hit","hot","dot","dog","cog"],
|
32 | 32 | * ["hit","hot","lot","log","cog"]
|
33 | 33 | * ]
|
34 |
| - * |
| 34 | + * |
35 | 35 | * Example 2:
|
36 |
| - * |
| 36 | + * |
37 | 37 | * Input:
|
38 | 38 | * beginWord = "hit"
|
39 | 39 | * endWord = "cog"
|
40 | 40 | * wordList = ["hot","dot","dog","lot","log"]
|
41 |
| - * |
| 41 | + * |
42 | 42 | * Output: []
|
43 |
| - * |
| 43 | + * |
44 | 44 | * Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.
|
45 |
| - * |
| 45 | + * |
46 | 46 | ******************************************************************************************************/
|
47 | 47 |
|
48 |
| -#include <vector> |
49 |
| -#include <string> |
50 |
| -#include <unordered_set> |
51 |
| -#include <unordered_map> |
52 |
| -#include <queue> |
53 |
| - |
54 |
| -using namespace std; |
| 48 | +#include "../inc/ac.h" |
55 | 49 |
|
56 |
| -class Solution { |
| 50 | +class Solution1 |
| 51 | +{ |
57 | 52 | public:
|
58 |
| - vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) { |
| 53 | + vector<vector<string>> findLadders(string beginWord, string endWord, vector<string> &wordList) |
| 54 | + { |
59 | 55 | vector<vector<string>> res;
|
60 |
| - unordered_set<string> visit; //notice we need to clear visited word in list after finish this level of BFS |
| 56 | + unordered_set<string> visit; // notice we need to clear visited word in list after finish this level of BFS |
61 | 57 | queue<vector<string>> q;
|
62 | 58 | unordered_set<string> wordlist(wordList.begin(), wordList.end());
|
63 |
| - q.push({ beginWord }); |
64 |
| - bool flag = false; //to see if we find shortest path |
65 |
| - while (!q.empty()) { |
| 59 | + q.push({beginWord}); |
| 60 | + bool flag = false; // to see if we find shortest path |
| 61 | + while (!q.empty()) |
| 62 | + { |
66 | 63 | int size = q.size();
|
67 |
| - for (int i = 0; i<size; i++) { //for this level |
| 64 | + for (int i = 0; i < size; i++) |
| 65 | + { // for this level |
68 | 66 | vector<string> cur = q.front();
|
69 | 67 | q.pop();
|
70 | 68 | vector<string> newadd = addWord(cur.back(), wordlist);
|
71 |
| - for (int j = 0; j<newadd.size(); j++) { //add a word into path |
| 69 | + for (int j = 0; j < newadd.size(); j++) |
| 70 | + { // add a word into path |
72 | 71 | vector<string> newline(cur.begin(), cur.end());
|
73 | 72 | newline.push_back(newadd[j]);
|
74 |
| - if (newadd[j] == endWord) { |
| 73 | + if (newadd[j] == endWord) |
| 74 | + { |
75 | 75 | flag = true;
|
76 | 76 | res.push_back(newline);
|
77 | 77 | }
|
78 | 78 | visit.insert(newadd[j]);
|
79 | 79 | q.push(newline);
|
80 | 80 | }
|
81 | 81 | }
|
82 |
| - if (flag) break; //do not BFS further |
83 |
| - for (auto it = visit.begin(); it != visit.end(); it++) wordlist.erase(*it); //erase visited one |
| 82 | + if (flag) |
| 83 | + break; // do not BFS further |
| 84 | + for (auto it = visit.begin(); it != visit.end(); it++) |
| 85 | + wordlist.erase(*it); // erase visited one |
84 | 86 | visit.clear();
|
85 | 87 | }
|
86 | 88 | return res;
|
87 | 89 | }
|
88 | 90 |
|
89 | 91 | // find words with one char different in dict
|
90 | 92 | // hot->[dot,lot]
|
91 |
| - vector<string> addWord(string word, unordered_set<string>& wordlist) { |
| 93 | + vector<string> addWord(string word, unordered_set<string> &wordlist) |
| 94 | + { |
92 | 95 | vector<string> res;
|
93 |
| - for (int i = 0; i<word.size(); i++) { |
| 96 | + for (int i = 0; i < word.size(); i++) |
| 97 | + { |
94 | 98 | char s = word[i];
|
95 |
| - for (char c = 'a'; c <= 'z'; c++) { |
| 99 | + for (char c = 'a'; c <= 'z'; c++) |
| 100 | + { |
96 | 101 | word[i] = c;
|
97 |
| - if (wordlist.count(word)) res.push_back(word); |
| 102 | + if (wordlist.count(word)) |
| 103 | + res.push_back(word); |
98 | 104 | }
|
99 | 105 | word[i] = s;
|
100 | 106 | }
|
101 | 107 | return res;
|
102 | 108 | }
|
103 | 109 | };
|
104 | 110 |
|
| 111 | +class Solution |
| 112 | +{ |
| 113 | +public: |
| 114 | + vector<vector<string>> findLadders(string beginWord, string endWord, vector<string> &wordList) |
| 115 | + { |
| 116 | + set<string> dict(wordList.begin(), wordList.end()); |
105 | 117 |
|
| 118 | + queue<vector<string>> que; |
| 119 | + que.push({beginWord}); |
| 120 | + vector<vector<string>> ans; |
106 | 121 |
|
107 |
| -//#if DEBUG |
108 |
| -int main(int argc, char** argv) { |
| 122 | + while (!que.empty()) |
| 123 | + { |
| 124 | + int len = que.size(); |
| 125 | + for (int j = 0; j < len; j++) |
| 126 | + { |
| 127 | + vector<string> seq = que.front(); |
| 128 | + que.pop(); |
| 129 | + |
| 130 | + // Insert a path solution. |
| 131 | + if (seq.back() == endWord) |
| 132 | + { |
| 133 | + ans.push_back(seq); |
| 134 | + continue; |
| 135 | + } |
109 | 136 |
|
110 |
| - Solution a; |
| 137 | + // Find neighbors. |
| 138 | + string word = seq.back(); |
| 139 | + for (int i = 0; i < word.size(); i++) |
| 140 | + { |
| 141 | + string newWord = seq.back(); |
| 142 | + for (char ch = 'a'; ch <= 'z'; ch++) |
| 143 | + { |
| 144 | + newWord[i] = ch; |
| 145 | + |
| 146 | + if (!dict.count(newWord)) |
| 147 | + { |
| 148 | + continue; |
| 149 | + } |
| 150 | + |
| 151 | + dict.erase(word); |
| 152 | + que.push(seq); |
| 153 | + que.back().push_back(newWord); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + if (not ans.empty()) |
| 159 | + { |
| 160 | + break; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + return ans; |
| 165 | + } |
| 166 | +}; |
| 167 | + |
| 168 | +//#if DEBUG |
| 169 | +int main(int argc, char **argv) |
| 170 | +{ |
111 | 171 |
|
112 | 172 | string beginWord = "hit", endWord = "cog";
|
113 |
| - vector<string> wordList = { "hot", "dot", "dog", "lot", "log", "cog" }; |
114 |
| - |
115 |
| - a.findLadders(beginWord, endWord, wordList); |
| 173 | + vector<string> wordList = {"hot", "dot", "dog", "lot", "log", "cog"}; |
| 174 | + |
| 175 | + auto ans = Solution().findLadders(beginWord, endWord, wordList); |
| 176 | + for (auto &&iter : ans) |
| 177 | + { |
| 178 | + for (auto &&subiter : iter) |
| 179 | + { |
| 180 | + cout << subiter << " "; |
| 181 | + } |
| 182 | + cout << "\n"; |
| 183 | + } |
| 184 | + cout << "\n"; |
| 185 | + |
116 | 186 | return 0;
|
117 | 187 | }
|
118 | 188 | //#endif
|
0 commit comments