Skip to content

Commit 26069de

Browse files
committed
Add designAddAndSearchWords solution
1 parent ab551a4 commit 26069de

File tree

1 file changed

+37
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)