-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyramidTransitionMatrix.cpp
More file actions
30 lines (28 loc) · 945 Bytes
/
pyramidTransitionMatrix.cpp
File metadata and controls
30 lines (28 loc) · 945 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
// Source: https://leetcode.com/problems/pyramid-transition-matrix/
// Author: Miao Zhang
// Date: 2021-03-08
class Solution {
public:
bool pyramidTransition(string bottom, vector<string>& allowed) {
unordered_map<string, unordered_set<char>> dic;
for (string str: allowed) {
dic[str.substr(0, 2)].insert(str[2]);
}
return dfs(bottom, "", dic);
}
private:
bool dfs(string cur, string above, unordered_map<string, unordered_set<char>>& dic) {
if (cur.size() == 2 && above.size() == 1) return true;
if (above.size() == cur.size() - 1) return dfs(above, "", dic);
int pos = above.size();
string base = cur.substr(pos, 2);
if (dic.count(base)) {
for (char c: dic[base]) {
if (dfs(cur, above + string(1, c), dic)) {
return true;
}
}
}
return false;
}
};