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+
7+ class TrieNode {
8+ constructor ( ) {
9+ this . children = { } ;
10+ this . isEnd = false ;
11+ }
12+ }
13+
14+ function isConsistent ( phoneNumbers ) {
15+ const root = new TrieNode ( ) ;
16+
17+ for ( const number of phoneNumbers ) {
18+ let node = root ;
19+
20+ for ( let i = 0 ; i < number . length ; i ++ ) {
21+ const char = number [ i ] ;
22+
23+ if ( ! node . children [ char ] ) {
24+ node . children [ char ] = new TrieNode ( ) ;
25+ }
26+
27+ node = node . children [ char ] ;
28+
29+ if ( node . isEnd ) {
30+ return false ;
31+ }
32+ }
33+ if ( Object . keys ( node . children ) . length > 0 ) {
34+ return false ;
35+ }
36+ node . isEnd = true ;
37+ }
38+
39+ return true ;
40+ }
41+
42+ function solution ( input ) {
43+ const t = parseInt ( input [ 0 ] ) ;
44+ let index = 1 ;
45+
46+ for ( let i = 0 ; i < t ; i ++ ) {
47+ const n = parseInt ( input [ index ] ) ;
48+ const phoneNumbers = input . slice ( index + 1 , index + 1 + n ) ;
49+ isConsistent ( phoneNumbers ) ? console . log ( 'YES' ) : console . log ( 'NO' ) ;
50+
51+ index += n + 1 ;
52+ }
53+ }
54+
55+ solution ( input ) ;
You can’t perform that action at this time.
0 commit comments