File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
design-add-and-search-words-data-structure Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ var WordDictionary = function ( ) {
3+ const dictionary = new Set ( '' ) ;
4+
5+ this . dictionary = dictionary
6+ } ;
7+
8+ /**
9+ * @param {string } word
10+ * @return {void }
11+ */
12+ WordDictionary . prototype . addWord = function ( word ) {
13+ this . dictionary . add ( word ) ;
14+ } ;
15+
16+ /**
17+ * @param {string } word
18+ * @return {boolean }
19+ */
20+ WordDictionary . prototype . search = function ( word ) {
21+ if ( word . includes ( '.' ) ) {
22+ const dictionaryList = [ ...this . dictionary ] ;
23+
24+ return dictionaryList . some ( ( dic ) => {
25+ if ( dic . length !== word . length ) return false ;
26+
27+ return [ ...dic ] . every ( ( char , index ) => word [ index ] === '.' ? true : word [ index ] === char )
28+ } )
29+ } ;
30+
31+ return this . dictionary . has ( word )
32+ } ;
33+
34+ /**
35+ * Your WordDictionary object will be instantiated and called as such:
36+ * var obj = new WordDictionary()
37+ * obj.addWord(word)
38+ * var param_2 = obj.search(word)
39+ */
You can’t perform that action at this time.
0 commit comments