Shu-How Zの小窝

Loading...

Leetcode211 添加与搜索单词


var WordDictionary = function() {
    this.words=[]
};

/** 
 * @param {string} word
 * @return {void}
 */
WordDictionary.prototype.addWord = function(word) {
    let wordLen=word.length

    if(this.words[wordLen]==undefined){
        this.words[wordLen]=[word]
    }else{
        this.words[wordLen].push(word)
    }
};

/** 
 * @param {string} word
 * @return {boolean}
 */
WordDictionary.prototype.search = function(word) {
    let wordLen=word.length

    if(this.words[wordLen]==undefined){
        return false
    }

    if(!word.includes('.')){
        return this.words[wordLen].includes(word)
    }

    let reg=new RegExp(word)
    return this.words[wordLen].some(i=>reg.test(i))
};

/** 
 * Your WordDictionary object will be instantiated and called as such:
 * var obj = new WordDictionary()
 * obj.addWord(word)
 * var param_2 = obj.search(word)
 */

不能全部通过

posted @ 2026-08-26 22:34  KooTeam  阅读(3)  评论(0)    收藏  举报