Skip to content

Commit a1aa5f6

Browse files
committed
Time: 4 ms (84.10%), Space: 8.7 MB (47.18%) - LeetHub
1 parent 493ae34 commit a1aa5f6

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
unordered_map<string, vector<char>> mp;
3+
public:
4+
5+
bool dfs(string bottom, string tmp){
6+
// base
7+
if(bottom.size() == 1)
8+
return true;
9+
if(bottom.size() == tmp.size() + 1)
10+
return dfs(tmp, "");
11+
12+
// main
13+
for(auto ch: mp[bottom.substr(tmp.size(), 2)]){
14+
if(dfs(bottom, tmp + ch))
15+
return true;
16+
}
17+
18+
return false;
19+
}
20+
21+
bool pyramidTransition(string bottom, vector<string>& allowed) {
22+
for(auto str: allowed){
23+
mp[str.substr(0, 2)].push_back(str[2]);
24+
}
25+
26+
return dfs(bottom, "");
27+
}
28+
};

0 commit comments

Comments
 (0)