File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
design-add-and-search-words-data-structure Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ var WordDictionary = function ( ) {
2+ this . dictionary = new Set ( ) ;
3+ } ;
4+
5+ /**
6+ * @param {string } word
7+ * @return {void }
8+ */
9+ WordDictionary . prototype . addWord = function ( word ) {
10+ this . dictionary . add ( word ) ;
11+ } ;
12+
13+ /**
14+ * @param {string } word
15+ * @return {boolean }
16+ */
17+ WordDictionary . prototype . search = function ( word ) {
18+ if ( word . indexOf ( "." ) != - 1 ) {
19+ // Case of word has a '.'
20+ for ( let str of this . dictionary ) {
21+ if ( str . length != word . length ) continue ;
22+ let i ;
23+ for ( i = 0 ; i < word . length ; i ++ ) {
24+ if ( word [ i ] === "." ) continue ;
25+ if ( word [ i ] != str [ i ] ) break ;
26+ }
27+ if ( i === str . length ) return true ;
28+ }
29+ return false ;
30+ } else {
31+ return this . dictionary . has ( word ) ;
32+ }
33+ } ;
34+
35+ // n: number of node | m: length of the word
36+ // TC: O(n*m)
37+ // SC: O(n*m)
You can’t perform that action at this time.
0 commit comments