/**
* Initialize your data structure here.
*/
var MagicDictionary = function() {
};
/**
* Build a dictionary through a list of words
* @param {string[]} dict
* @return {void}
*/
MagicDictionary.prototype.buildDict = function(dict) {
this.dict = dict;
};
/**
* Returns if there is any word in the trie that equals to the given word after modifying exactly one character
* @param {string} word
* @return {boolean}
*/
MagicDictionary.prototype.search = function(word) {
for(let i = 0; i < this.dict.length; ++i){
let obj = this.dict[i];
TAG:
if(obj.length === word.length){
let diffCount = 0;
for(let j = 0; j < obj.length; ++j){
if(obj[j] !== word[j]){
diffCount++;
if(diffCount > 1){
j = obj.length;
}
}
}
if(diffCount === 1){
return true;
}
}
else{
continue;
}
}
return false;
};
/**
* Your MagicDictionary object will be instantiated and called as such:
* var obj = Object.create(MagicDictionary).createNew()
* obj.buildDict(dict)
* var param_2 = obj.search(word)
*/
复制代码
转载于:https://juejin.im/post/5b701e8b518825610072aade