-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathindex.js
84 lines (73 loc) · 2.3 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
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
81
82
83
84
const { expect } = require('chai');
/**
* @param {string[]} words
*/
let StreamChecker = function(words) {
function Node() {
this.next = {};
this.finish = false;
}
this.root = new Node(null);
for (let word of words) {
let tmp = this.root;
for (let i = 0; i < word.length; i++) {
if (!tmp.next[word[i]]) {
tmp.next[word[i]] = new Node();
}
tmp = tmp.next[word[i]];
if (i === word.length - 1) tmp.finish = true;
}
}
this.curr = [];
};
/**
* @param {character} letter
* @return {boolean}
*/
StreamChecker.prototype.query = function(letter) {
const nextCurr = [];
let found = false;
for (let c of [...this.curr, this.root]) {
if (c.next[letter]) {
nextCurr.push(c.next[letter]);
if (c.next[letter].finish) found = true;
}
}
this.curr = nextCurr;
return found;
};
/**
* Your StreamChecker object will be instantiated and called as such:
* var obj = new StreamChecker(words)
* var param_1 = obj.query(letter)
*/
const streamChecker = new StreamChecker(['cd','f','kl']);
console.log(JSON.stringify(streamChecker.root));
const sc2 = new StreamChecker(['ab', 'ba', 'aaab', 'abab', 'baa']);
console.log(JSON.stringify(sc2.root));
const sc3 = new StreamChecker(['abaa','abaab','aabbb','bab','ab']);
console.log(JSON.stringify(sc3.root));
it('stream-of-characters', () => {
expect(streamChecker.query('a')).to.eq(false);
expect(streamChecker.query('b')).to.eq(false);
expect(streamChecker.query('c')).to.eq(false);
expect(streamChecker.query('d')).to.eq(true );
expect(streamChecker.query('e')).to.eq(false);
expect(streamChecker.query('f')).to.eq(true );
expect(streamChecker.query('g')).to.eq(false);
expect(streamChecker.query('h')).to.eq(false);
expect(streamChecker.query('i')).to.eq(false);
expect(streamChecker.query('j')).to.eq(false);
expect(streamChecker.query('k')).to.eq(false);
expect(streamChecker.query('l')).to.eq(true );
expect(sc2.query('a')).to.eq(false);
expect(sc2.query('a')).to.eq(false);
expect(sc2.query('a')).to.eq(false);
expect(sc2.query('a')).to.eq(false);
expect(sc2.query('a')).to.eq(false);
expect(sc2.query('b')).to.eq(true);
expect(sc2.query('a')).to.eq(true);
expect(sc3.query('a')).to.eq(false);
expect(sc3.query('a')).to.eq(false);
expect(sc3.query('b')).to.eq(true);
});