Skip to content

Commit 3a96ff1

Browse files
authored
Create 756-pyramid-transition-matrix.js
1 parent ff0e12e commit 3a96ff1

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

756-pyramid-transition-matrix.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {string} bottom
3+
* @param {string[]} allowed
4+
* @return {boolean}
5+
*/
6+
const pyramidTransition = function (bottom, allowed) {
7+
const map = new Map()
8+
allowed.map((item) => {
9+
const t = map.get(item[0] + item[1]) || new Array()
10+
t.push(item[2])
11+
map.set(item[0] + item[1], t)
12+
})
13+
const memo = new Map()
14+
const solve = function (cur, ind, next) {
15+
if (memo.has(cur)) return memo.get(cur)
16+
if (cur.length === 1) return true
17+
if (ind >= cur.length - 1) {
18+
const res = solve(next, 0, '')
19+
memo.set(next, res)
20+
return res
21+
}
22+
if (!map.has(cur.slice(ind, ind + 2))) {
23+
memo.set(cur, false)
24+
return false
25+
}
26+
for (let char of map.get(cur.slice(ind, ind + 2))) {
27+
if (solve(cur, ind + 1, next + char)) return true
28+
}
29+
return false
30+
}
31+
return solve(bottom, 0, '')
32+
}

0 commit comments

Comments
 (0)