-
Notifications
You must be signed in to change notification settings - Fork 2
/
1032.字符流.js
49 lines (45 loc) · 1001 Bytes
/
1032.字符流.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
/*
* @lc app=leetcode.cn id=1032 lang=javascript
*
* [1032] 字符流
*/
// @lc code=start
/**
* @param {string[]} words
*/
var StreamChecker = function(words) {
this.root = {};
for (let w of words) {
w = w.split('').reverse().join('');
let node = this.root;
for (let c of w) {
if (!node[c]) node[c] = {};
node = node[c];
}
node.word = w;
}
this.letters = [];
};
/**
* @param {character} letter
* @return {boolean}
*/
StreamChecker.prototype.query = function(letter) {
this.letters.push(letter);
let lookingIn = this.root;
for(let i = this.letters.length-1; i >=0; i--) {
if(lookingIn[this.letters[i]]) {
lookingIn = lookingIn[this.letters[i]];
if(lookingIn.word) return true;
} else {
return false;
}
}
return false
};
/**
* Your StreamChecker object will be instantiated and called as such:
* var obj = new StreamChecker(words)
* var param_1 = obj.query(letter)
*/
// @lc code=end