0
点赞
收藏
分享

微信扫一扫

211. Design Add and Search Words Data Structure

十日十月Freddie 2022-03-11 阅读 44
leetcode

题目:

Design a data structure that supports adding new words and finding if a string matches any previously added string.

Implement the WordDictionary class:

  • WordDictionary() Initializes the object.
  • void addWord(word) Adds word to the data structure, it can be matched later.
  • bool search(word) Returns true if there is any string in the data structure that matches word or false otherwise. word may contain dots '.' where dots can be matched with any letter.

Example:

Input
["WordDictionary","addWord","addWord","addWord","search","search","search","search"]
[[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]]
Output
[null,null,null,null,false,true,true,true]

Explanation
WordDictionary wordDictionary = new WordDictionary();
wordDictionary.addWord("bad");
wordDictionary.addWord("dad");
wordDictionary.addWord("mad");
wordDictionary.search("pad"); // return False
wordDictionary.search("bad"); // return True
wordDictionary.search(".ad"); // return True
wordDictionary.search("b.."); // return True

Constraints:

  • 1 <= word.length <= 25
  • word in addWord consists of lowercase English letters.
  • word in search consist of '.' or lowercase English letters.
  • There will be at most 3 dots in word for search queries.
  • At most 104 calls will be made to addWord and search.

思路:

这题string前缀,考虑字典树。唯一的要点是add的word一定是完整单词,不会有“.”,但是search里面的word是有“.”的。因为不知道会有多少“.”,search要写成递归。首先递归base case是当前的index已经和单词长度相等,检查当前的bool值即可。之后情况分类,如果当前字母是“.”,则26个字母都需要去尝试,这里可以剪枝一下,如果child[c]是空的就不用递归下去了,然后返回值用了一个或,只要26个字母中有任意true即可。

代码:

class WordDictionary {
public:
    WordDictionary () {
        trie = new Trie();
    }

    void addWord(string word) {
        insert(word);
    }

    bool search(string word) {
        return check(word, 0, trie);
    }
private:
    class Trie {
    public:
        Trie* child[26] = { nullptr };
        bool flag = false;
    };
    Trie* trie;
    void insert(string& word) {
        Trie* tmp = trie;
        for (int i = 0; i < word.size(); i++) {
            int c = word[i] - 'a';
            if (!tmp->child[c]) {
                tmp->child[c] = new Trie();
            }
            tmp = tmp->child[c];
        }
        tmp->flag = true;
    }
    bool check(string& word, int index, Trie* start) {
        if (index == word.size()) {
            if (start->flag)
                return true;
            else
                return false;
        }
        bool ans = false;
        if (word[index] == '.') {
            for (int i = 0; i < 26; i++) {
                if (start->child[i])
                    ans |= check(word, index + 1, start->child[i]);
            }
        }
        else {
            int c = word[index] - 'a';
            if (!start->child[c])
                return false;
            else {
                return check(word, index + 1, start->child[c]);
            }
        }
        return ans;
    }
};

举报

相关推荐

0 条评论