forked from ByteByteGoHq/coding-interview-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortest_transformation_sequence_optimized.cpp
73 lines (71 loc) · 2.95 KB
/
shortest_transformation_sequence_optimized.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <string>
#include <vector>
#include <deque>
#include <unordered_set>
int shortestTransformationSequenceOptimized(std::string& start, std::string& end, std::vector<std::string>& dictionary) {
std::unordered_set<std::string> dictionarySet(dictionary.begin(), dictionary.end());
if (dictionarySet.find(start) == dictionarySet.end() || dictionarySet.find(end) == dictionarySet.end()) {
return 0;
}
if (start == end) {
return 1;
}
std::deque<std::string> startQueue;
startQueue.push_back(start);
std::unordered_set<std::string> startVisited;
startVisited.insert(start);
std::deque<std::string> endQueue;
endQueue.push_back(end);
std::unordered_set<std::string> endVisited;
endVisited.insert(end);
int levelStart = 0, levelEnd = 0;
// Perform a level-order traversal from the start word and another
// from the end word.
while (!startQueue.empty() && !endQueue.empty()) {
// Explore the next level of the traversal that starts from the
// start word. If it meets the other traversal, the shortest
// path between 'start' and 'end' has been found.
levelStart++;
if (exploreLevel(startQueue, startVisited, endVisited, dictionarySet)) {
return levelStart + levelEnd + 1;
}
// Explore the next level of the traversal that starts from the
// end word.
levelEnd++;
if (exploreLevel(endQueue, endVisited, startVisited, dictionarySet)) {
return levelStart + levelEnd + 1;
}
}
// If the traversals never met, then no path exists.
return 0;
}
// This function explores the next level in the level-order traversal
// and checks if two searches meet.
bool exploreLevel(std::deque<std::string>& queue, std::unordered_set<std::string>& visited,
std::unordered_set<std::string>& otherVisited,
std::unordered_set<std::string>& dictionarySet) {
std::string lowerCaseAlphabet = "abcdefghijklmnopqrstuvwxyz";
int size = queue.size();
for (int unused = 0; unused < size; unused++) {
std::string currentWord = queue.front();
queue.pop_front();
for (int i = 0; i < currentWord.length(); i++) {
for (char c : lowerCaseAlphabet) {
std::string nextWord = currentWord;
nextWord[i] = c;
// If 'nextWord' has been visited during the other
// traversal, it means both searches have met.
if (otherVisited.find(nextWord) != otherVisited.end()) {
return true;
}
if (dictionarySet.find(nextWord) != dictionarySet.end() && visited.find(nextWord) == visited.end()) {
visited.insert(nextWord);
queue.push_back(nextWord);
}
}
}
}
// If no word has been visited by the other traversal, the searches
// have not met yet.
return false;
}