function Trie(c, done = false) {
this.dict = {};
}
var WordDictionary = function () {
this.dict = {};
};
/**
* @param {string} word
* @return {void}
*/
WordDictionary.prototype.addWord = function (word) {
let currentDict = this.dict;
for (let i = 0; i < word.length; i++) {
const c = word[i];
currentDict[c] = currentDict[c] ?? {};
currentDict = currentDict[c];
}
currentDict.done = true;
return null;
};
/**
* @param {string} word
* @return {boolean}
*/
WordDictionary.prototype.search = function (word, idx = 0, dict = this.dict) {
if (idx === word.length) {
return !!dict.done;
}
const c = word[idx];
if (c === '.') {
for (const [key, value] of Object.entries(dict)) {
const isValid = this.search(word, idx + 1, dict[key]);
if (isValid) return true;
}
} else {
if (dict[c] === undefined) return false;
return this.search(word, idx + 1, dict[c]);
}
return false;
};
/**
* Your WordDictionary object will be instantiated and called as such:
* var obj = new WordDictionary()
* obj.addWord(word)
* var param_2 = obj.search(word)
*/