-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path756.cpp
More file actions
30 lines (29 loc) · 853 Bytes
/
Copy path756.cpp
File metadata and controls
30 lines (29 loc) · 853 Bytes
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
class Solution {
public:
bool pyramidTransition(string bottom, vector<string> &allowed) {
unordered_map<char, vector<string>> tuple;
for (auto &a:allowed)
tuple[a[0]].push_back(a);
string upper;
return DFS(bottom, upper, tuple);
}
bool DFS(string bot, string upper, unordered_map<char, vector<string>> &tuple) {
if (bot.size() <= 1) {
if (upper.size() <= 1)
return true;
bot = upper;
upper.clear();
}
auto s = bot[0];
bot.erase(bot.begin());
for (auto l:tuple[s]) {
if (l[1] == bot[0]) {
upper.push_back(l[2]);
if (DFS(bot, upper, tuple))
return true;
upper.pop_back();
}
}
return false;
}
};