File tree Expand file tree Collapse file tree 3 files changed +103
-7
lines changed 
src/data-structures/graphs Expand file tree Collapse file tree 3 files changed +103
-7
lines changed Original file line number Diff line number Diff line change 1+ /** 
2+  * Your WordDictionary object will be instantiated and called as such: 
3+  * var obj = new WordDictionary() 
4+  * obj.addWord(word) 
5+  * var param_2 = obj.search(word) 
6+  */ 
7+ class  WordDictionary  { 
8+   children  =  { } ; 
9+   isWord  =  false ; 
10+   /** 
11+    * Initialize your data structure here. 
12+    */ 
13+   constructor ( )  { 
14+   } 
15+ 
16+   /** 
17+    * Adds a word into the data structure. 
18+    * @param  {string } word 
19+    * @return  {void } 
20+    */ 
21+   addWord  ( word )  { 
22+     let  curr  =  this ; 
23+ 
24+     for  ( let  char  of  word )  { 
25+       if  ( ! curr . children [ char ] )  curr . children [ char ]  =  new  WordDictionary ( ) ; 
26+       curr  =  curr . children [ char ] ; 
27+     } 
28+ 
29+     curr . isWord  =  true ; 
30+   } 
31+ 
32+   /** 
33+    * Returns if the word is in the data structure. 
34+    * A word could contain the dot character '.' to represent any one letter. 
35+    * @param  {string } word 
36+    * @return  {boolean } 
37+    */ 
38+   search  ( word ,  curr  =  this ,  index  =  0 )  { 
39+     if  ( index  >  word . length )  return  true ;  // e.g. final '.' 
40+     for  ( let  [ i ,  char ]  of  [ ...word . slice ( index ) ] . entries ( ) )  { 
41+       if  ( char  ===  '.' )  { 
42+         for  ( let  child  of  Object . keys ( curr . children ) )  { 
43+           if  ( this . search ( word ,  curr . children [ child ] ,  i  +  1 ) )  return  true ; 
44+         } 
45+       } 
46+       else  if  ( ! curr  ||  ! curr . children [ char ] )  return  false ; 
47+       curr  =  curr . children [ char ] ; 
48+     } 
49+ 
50+     return  curr . isWord ; 
51+   } 
52+ } 
53+ 
54+ module . exports  =  WordDictionary ; 
Original file line number Diff line number Diff line change 1+ const  WordDictionary  =  require ( './trie-wildcard-search' ) ; 
2+ 
3+ describe ( 'WordDictionary' ,  ( )  =>  { 
4+   let  wd ; 
5+ 
6+   beforeEach ( ( )  =>  { 
7+     wd  =  new  WordDictionary ( ) ; 
8+   } ) ; 
9+ 
10+   describe ( 'should find exact matches' ,  ( )  =>  { 
11+     beforeEach ( ( )  =>  { 
12+       wd . addWord ( 'bad' ) ; 
13+       wd . addWord ( 'mad' ) ; 
14+     } ) ; 
15+ 
16+     it ( 'should find match' ,  ( )  =>  { 
17+       expect ( wd . search ( 'bad' ) ) . toEqual ( true ) ; 
18+       expect ( wd . search ( 'mad' ) ) . toEqual ( true ) ; 
19+     } ) ; 
20+ 
21+     it ( 'should be false for partial match' ,  ( )  =>  { 
22+       expect ( wd . search ( 'ba' ) ) . toEqual ( false ) ; 
23+     } ) ; 
24+ 
25+     it ( 'should be false for NO match' ,  ( )  =>  { 
26+       expect ( wd . search ( 'dad' ) ) . toEqual ( false ) ; 
27+     } ) ; 
28+   } ) ; 
29+ 
30+   describe ( 'should find wildcard matches' ,  ( )  =>  { 
31+     beforeEach ( ( )  =>  { 
32+       wd . addWord ( 'bad' ) ; 
33+     } ) ; 
34+ 
35+     it ( 'should work with 1 wildcard' ,  ( )  =>  { 
36+       expect ( wd . search ( '.ad' ) ) . toEqual ( true ) ; 
37+     } ) ; 
38+ 
39+     it ( 'should work with 1 wildcard not match' ,  ( )  =>  { 
40+       expect ( wd . search ( '.ax' ) ) . toEqual ( false ) ; 
41+     } ) ; 
42+ 
43+     it ( 'should work with 2 wildcard' ,  ( )  =>  { 
44+       expect ( wd . search ( '..d' ) ) . toEqual ( true ) ; 
45+     } ) ; 
46+   } ) ; 
47+ } ) ; 
Original file line number Diff line number Diff line change @@ -250,12 +250,7 @@ class Graph {
250250  } 
251251} 
252252
253- Graph . UNDIRECTED  =  Symbol ( 'undirected  graph' ) ;  // one-way  edges 
254- Graph . DIRECTED  =  Symbol ( 'directed  graph' ) ;  // two-ways  edges 
253+ Graph . UNDIRECTED  =  Symbol ( 'directed  graph' ) ;  // two-ways  edges 
254+ Graph . DIRECTED  =  Symbol ( 'undirected  graph' ) ;  // one-way  edges 
255255
256256module . exports  =  Graph ; 
257- 
258- /* 
259-  * https://repl.it/@amejiarosario/graphpy 
260-  * http://www.pythontutor.com/visualize.html#mode=edit - https://goo.gl/Xp7Zpm 
261-  */ 
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments