-
Notifications
You must be signed in to change notification settings - Fork 0
/
211.添加与搜索单词-数据结构设计.js
79 lines (69 loc) · 1.84 KB
/
211.添加与搜索单词-数据结构设计.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
/*
* @lc app=leetcode.cn id=211 lang=javascript
*
* [211] 添加与搜索单词 - 数据结构设计
*/
// @lc code=start
/**
* Initialize your data structure here.
*/
var WordDictionary = function() {
this.root = new TrilNode();
};
/**
* @param {string} word
* @return {void}
*/
WordDictionary.prototype.addWord = function(word) {
let node = this.root;
let len = word.length
for (let i = 0; i < len; i++) {
const letter = word[i];
if (!node.children[letter]) {
node.children[letter] = new TrilNode();
};
node = node.children[letter];
}
node.isEndingChart = true;
};
/**
* @param {string} word
* @return {boolean}
*/
WordDictionary.prototype.search = function(word, parent) {
let node = parent || this.root;
const len = word.length;
for (let i = 0; i < len; i++) {
const letter = word[i];
if (letter === '.') {
const childs = Object.values(node.children);
// 如果是最后一个元素
if (i === len - 1) {
// 最后一个元素必须是结尾元素
return !!childs.length && childs.some(item => item.isEndingChart);
}
let flag = false;
// 回溯,只要有一个符合条件,则为查找成功
for (let j = 0; j < childs.length; j++) {
const result = this.search(word.slice(i + 1), childs[j]);
if (result) flag = true;
}
return flag;
} else {
if (!node.children[letter]) return false;
node = node.children[letter];
}
}
return node.isEndingChart;
};
var TrilNode = function() {
this.children = {};
this.isEndingChart = false;
}
/**
* Your WordDictionary object will be instantiated and called as such:
* var obj = new WordDictionary()
* obj.addWord(word)
* var param_2 = obj.search(word)
*/
// @lc code=end