File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments