typescript-algorithms-and-datastructures
Version:
Useful algorithms and Data structures written in typescript.
52 lines • 1.59 kB
JavaScript
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class Trie {
constructor() {
this.root = {};
}
insert(word) {
const wordwSize = word.length;
let i = 0;
let level = this.root;
while (i < wordwSize) {
if (!level[word[i]]) {
level[word[i]] = {};
}
level = level[word[i]];
i++;
if (i === wordwSize) {
level[Trie.KEY] = true;
}
}
return true;
}
contains(word) {
const wordwSize = word.length;
let i = 0;
let level = this.root;
while (i < wordwSize) {
if (!level[word[i]]) {
return false;
}
level = level[word[i]];
i++;
if (i === wordwSize && level[Trie.KEY]) {
return true;
}
}
return false;
}
}
exports.Trie = Trie;
Trie.KEY = "id";
});
//# sourceMappingURL=Trie.js.map