class TrieNode {
constructor() {
this.key = null;
this.children = new Map();
this.value = null;
}
set(key, value) {
let node = this;
for (const k of key) {
if (!node.children.has(k)) {
node.children.set(k, new TrieNode());
}
node = node.children.get(k);
}
node.value = value;
}
increment(key) {
let node = this;
for (const k of key) {
node.value = (node.value === null ? 0 : node.value) + 1;
if (!node.children.has(k)) {
node.children.set(k, new TrieNode());
}
node = node.children.get(k);
}
node.value = (node.value === null ? 0 : node.value) + 1;
}
get(key) {
let node = this;
for (const k of key) {
if (!node.children.has(k)) {
return null;
}
node = node.children.get(k);
}
return node.value;
}
has(key) {
let node = this;
for (const k of key) {
if (!node.children.has(k)) {
return false;
}
node = node.children.get(k);
}
return true;
}
}