File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ const input = require ( 'fs' )
2+ . readFileSync ( process . platform === 'linux' ? '/dev/stdin' : './input.txt' )
3+ . toString ( )
4+ . trim ( )
5+ . split ( '\n' )
6+ . map ( ( el ) => el . split ( ' ' ) ) ;
7+
8+ function canTransform ( S , T ) {
9+ function dfs ( current ) {
10+ if ( current . length < S . length ) return false ;
11+ if ( current === S ) return true ;
12+
13+ let result = false ;
14+
15+ if ( current [ current . length - 1 ] === 'A' ) {
16+ result = result || dfs ( current . slice ( 0 , - 1 ) ) ;
17+ }
18+
19+ if ( current [ 0 ] === 'B' ) {
20+ const reversed = current . slice ( 1 ) . split ( '' ) . reverse ( ) . join ( '' ) ;
21+ result = result || dfs ( reversed ) ;
22+ }
23+
24+ return result ;
25+ }
26+
27+ return dfs ( T ) ? 1 : 0 ;
28+ }
29+
30+ function solution ( input ) {
31+ const S = input [ 0 ] [ 0 ] ;
32+ const T = input [ 1 ] [ 0 ] ;
33+
34+ console . log ( canTransform ( S , T ) ) ;
35+ }
36+
37+ solution ( input ) ;
You can’t perform that action at this time.
0 commit comments