class PreTree{
constructor(words=[]) {
this.pass = 0
this.end = 0
this.nexts = new Map()
for(let i in words) this.insert(words[i])
}
insert(word) {
if (!word) return
let cur = this
cur.pass++
for (let s of word) {
if (!cur.nexts.get(s)) {
cur.nexts.set(s, new PreTree())
}
cur = cur.nexts.get(s)
cur.pass++
}
cur.end++
}
delete(word) {
if (this.search(word)>0) {
let cur = this
cur.pass--
for (let s of word) {
if(--cur.nexts.get(s).pass==0) return cur.nexts.delete(s)
cur = cur.nexts.get(s)
}
cur.end--
}
}
search(word) {
if (!word) return 0
let cur = this
for (let s of word) {
if (!cur.nexts.has(s)) return 0
cur = cur.nexts.get(s)
}
return cur.end
}
prefixNumber(pre) {
if (!pre) return
let cur = this
for (let s of pre) {
if (!cur.nexts.has(s)) return 0
cur = cur.nexts.get(s)
}
return cur.pass
}
}