@@ -7,33 +7,33 @@ import (
77func pyramidTransition (bottom string , allowed []string ) bool {
88 blocks := "#" + strings .Join (allowed , "#" )
99
10- return helper ( bottom , blocks , 0 , len ( bottom ))
11- }
12-
13- func helper ( bottom , blocks string , curr , length int ) bool {
14- if curr + 2 > length {
15- bottom = bottom [ length :]
16- length = len ( bottom )
17- if length == 1 {
18- return true
10+ var helper func ( string , string , int , int ) bool
11+ helper = func ( bottom , blocks string , curr , length int ) bool {
12+ if curr + 2 > length {
13+ bottom = bottom [ length :]
14+ length = len ( bottom )
15+ if length == 1 {
16+ return true
17+ }
18+ return helper ( bottom , blocks , 0 , length )
1919 }
20- return helper (bottom , blocks , 0 , length )
21- }
2220
23- b := "#" + bottom [curr :curr + 2 ]
24-
25- count := strings . Count (blocks , b )
26-
27- for i := 0 ; i < count ; i ++ {
28- index := strings . Index ( blocks , b )
29- blk := blocks [ index : index + 4 ]
30- color := blk [ 3 :]
31- blocks = strings . Replace ( blocks , blk , "" , 1 )
32- blocks += blk
33- if helper ( bottom + color , blocks , curr + 1 , length ) {
34- return true
21+ b := "#" + bottom [curr :curr + 2 ]
22+ beg := 0
23+ for beg < len (blocks ) {
24+ index := strings . Index ( blocks [ beg :], b ) + beg
25+ if index < beg {
26+ break
27+ }
28+ beg = index + 4
29+ color := blocks [ index + 3 : index + 4 ]
30+ if helper ( bottom + color , blocks , curr + 1 , length ) {
31+ return true
32+ }
3533 }
34+
35+ return false
3636 }
3737
38- return false
38+ return helper ( bottom , blocks , 0 , len ( bottom ))
3939}
0 commit comments