Skip to content

Commit 78e3604

Browse files
authored
feat: design-add-and-search-words
1 parent 88fe786 commit 78e3604

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
*/

0 commit comments

Comments
 (0)