-
Notifications
You must be signed in to change notification settings - Fork 181
/
26.word-dictionary.js
80 lines (66 loc) · 2.23 KB
/
26.word-dictionary.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// https://leetcode-cn.com/problems/design-add-and-search-words-data-structure/
/**
* 211. 添加与搜索单词 - 数据结构设计
*
* 请你设计一个数据结构,支持 添加新单词 和 查找字符串是否与任何先前添加的字符串匹配 。
实现词典类 WordDictionary :
WordDictionary() 初始化词典对象
void addWord(word) 将 word 添加到数据结构中,之后可以对它进行匹配
bool search(word) 如果数据结构中存在字符串与 word 匹配,则返回 true ;否则,返回 false 。word 中可能包含一些 '.' ,每个 . 都可以表示任何一个字母。
示例:
输入:
["WordDictionary","addWord","addWord","addWord","search","search","search","search"]
[[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]]
输出:
[null,null,null,null,false,true,true,true]
解释:
WordDictionary wordDictionary = new WordDictionary();
wordDictionary.addWord("bad");
wordDictionary.addWord("dad");
wordDictionary.addWord("mad");
wordDictionary.search("pad"); // return False
wordDictionary.search("bad"); // return True
wordDictionary.search(".ad"); // return True
wordDictionary.search("b.."); // return True
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/design-add-and-search-words-data-structure
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
/**
* Initialize your data structure here.
*/
var WordDictionary = function () {
this.wordMap = {}
};
/**
* @param {string} word
* @return {void}
*/
WordDictionary.prototype.addWord = function (word) {
const len = word.length
const list = this.wordMap[ len ] || []
this.wordMap[ len ] = [ ...list, word ]
};
/**
* @param {string} word
* @return {boolean}
*/
WordDictionary.prototype.search = function (word) {
const len = word.length
if (!this.wordMap[ len ]) {
return false
}
if (word.includes('.')) {
const wordReg = new RegExp(word)
return this.wordMap[ len ].some((it) => wordReg.test(it))
} else {
return this.wordMap[ len ].includes(word)
}
};
/**
* Your WordDictionary object will be instantiated and called as such:
* var obj = new WordDictionary()
* obj.addWord(word)
* var param_2 = obj.search(word)
*/