-
Notifications
You must be signed in to change notification settings - Fork 0
/
trieSuffix.js
41 lines (35 loc) · 855 Bytes
/
trieSuffix.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
class trieNode{
constructor(){
this.children={}
this.end=false
}
}
class trie{
constructor(word){
this.root=new trieNode
for(let i=0;i<word.length;i++){
this.insert(word.substring(i))
}
}
insert(text){
let node=this.root
for(let i=0;i<text.length;i++){
let char=text[i]
if(!node.children[char]){
node.children[char]=new trieNode
}node=node.children[char]
}node.end=true
}
suffix(pattern){
let node=this.root
for(let i=0;i<pattern.length;i++){
let char=pattern[i]
if(!node.children[char]){
return false
}
node=node.children[char]
}return true
}
}
let tr=new trie("azhar")
console.log(tr.suffix("zha"));