js_algorithm icon indicating copy to clipboard operation
js_algorithm copied to clipboard

添加与搜索单词 - 数据结构设计

Open Cosen95 opened this issue 4 years ago • 1 comments

leetcode: https://leetcode-cn.com/problems/add-and-search-word-data-structure-design/

Cosen95 avatar May 03 '20 09:05 Cosen95

思路分析

这道题要求字符串既可以被添加、又可以被搜索,这就意味着字符串在添加时一定要被存在某处。键值对存储,我们用 Map(或对象字面量来模拟 Map)。

注意,这里为了降低查找时的复杂度,我们可以考虑以字符串的长度为 key,相同长度的字符串存在一个数组中,这样可以提高我们后续定位的效率。

难点在于 search 这个 API,它既可以搜索文字,又可以搜索正则表达式。因此我们在搜索前需要额外判断一下,传入的到底是普通字符串,还是正则表达式。若是普通字符串,则直接去 Map 中查找是否有这个 key;若是正则表达式,则创建一个正则表达式对象,判断 Map 中相同长度的字符串里,是否存在一个能够与这个正则相匹配。

这里需要大家复习一下正则表达式的创建,以及用于测试某个字符串是否与之匹配的方法

编码实现

/**
 * Initialize your data structure here.
 */
var WordDictionary = function() {
    // 初始化一个对象字面量,承担 Map 的角色
    this.words = {}
};

/**
 * Adds a word into the data structure. 
 * @param {string} word
 * @return {void}
 */
WordDictionary.prototype.addWord = function(word) {
    // 若该字符串对应长度的数组已经存在,则只做添加
    const len = word.length;
    if(this.words[len]) {
        this.words[len].push(word)
    } else {
        // 若改字符串对应长度的数组还不存在,则先创建
        this.words[len] = [word]
    }
};

/**
 * Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. 
 * @param {string} word
 * @return {boolean}
 */
WordDictionary.prototype.search = function(word) {
    // 若该字符串长度在 Map 中对应的数组根本不存在,则可判断该字符串不存在
    if(!this.words[word.length]) {
        return false
    }
    const len = word.length;
    // 如果字符串中不包含‘.’,那么一定是普通字符串
    if(!word.includes('.')) {
        // 定位到和目标字符串长度一致的字符串数组,在其中查找是否存在该字符串
        return this.words[len].includes(word)
    }

    // 否则是正则表达式,要先创建正则表达式对象
    const reg = new RegExp(word)

    return this.words[len].some((item) => {
        return reg.test(item)
    })
};

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

Cosen95 avatar May 03 '20 09:05 Cosen95