-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
49 lines (43 loc) · 1.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* Initialize your data structure here.
*/
var WordDictionary = function() {
this.map = {};
this.indexMap = {};
};
/**
* Adds a word into the data structure.
* @param {string} word
* @return {void}
*/
WordDictionary.prototype.addWord = function(word) {
this.map[word] = true;
let len = word.length;
if(!this.indexMap[len]){
this.indexMap[len] = [];
}
this.indexMap[len].push(word);
};
/**
* Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter.
* @param {string} word
* @return {boolean}
*/
WordDictionary.prototype.search = function(word) {
var rg = new RegExp('^' + word + '$');
let len = word.length;
let arr = this.indexMap[len];
if(!arr || arr.length === 0) return false;
for(var i = 0; i<arr.length; i++){
if(rg.test(arr[i])){
return true;
}
}
return false;
};
/**
* Your WordDictionary object will be instantiated and called as such:
* var obj = Object.create(WordDictionary).createNew()
* obj.addWord(word)
* var param_2 = obj.search(word)
*/