【LeetCode 面试经典150题】211. Design Add and Search Words Data Structure 设计添加与查找单词数据结构

211. Design Add and Search Words Data Structure(设计添加与查找单词数据结构)

题目大意

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.

中文释义

设计一个数据结构,支持添加新单词并查找字符串是否与之前添加的任何字符串匹配。

实现 WordDictionary 类:

  • WordDictionary() 初始化对象。
  • void addWord(word) 将单词添加到数据结构中,稍后可以进行匹配。
  • bool search(word) 如果数据结构中有任何字符串与 word 匹配,则返回 true,否则返回 false。word 可能包含点 ‘.’,其中点可以与任何字母匹配。

示例

  • 输入:
    • [“WordDictionary”, “addWord”, “addWord”, “addWord”, “search”, “search”, “search”, “search”]
    • [[], [“bad”], [“dad”], [“mad”], [“pad”], [“bad”], [“.ad”], [“b…”]]
  • 输出:
    • [null, null, null, null, false, true, true, true]
  • 解释:
    • WordDictionary wordDictionary = new WordDictionary();
    • wordDictionary.addWord(“bad”);
    • wordDictionary.addWord(“dad”);
    • wordDictionary.addWord(“mad”);
    • wordDictionary.search(“pad”); // 返回 False
    • wordDictionary.search(“bad”); // 返回 True
    • wordDictionary.search(“.ad”); // 返回 True
    • wordDictionary.search(“b…”); // 返回 True

限制条件

  • 1 <= word.length <= 25
  • addWord 中的 word 仅由小写英文字母组成。
  • search 中的 word 由 ‘.’ 或小写英文字母组成。
  • 搜索查询中的 word 最多包含 2 个点。
  • 对 addWord 和 search 的调用总次数最多为 104 次。

实现

实现 WordDictionary 类,包括 TrieNode 子类和 WordDictionary 方法。

TrieNode 类

  • 表示 WordDictionary 中的一个节点。
  • 包含一个布尔值 isEnd,指示该节点是否是一个单词的结尾。
  • 包含一个 TrieNode 类型的数组 children,表示子节点。

WordDictionary 类

  • 包含一个 TrieNode 类型的根节点 root
  • addWord 方法,用于添加一个单词。
  • search 方法,用于搜索一个单词,支持使用 ‘.’ 作为通配符。
  • searchInNode 辅助方法,用于在 Trie 结构中进行搜索。

代码

class TrieNode {
public:
    bool isEnd;
    TrieNode* children[26];
    TrieNode() {
        isEnd = false;
        for (int i = 0; i < 26; i++) {children[i] = nullptr;}
    }
};

class WordDictionary {
public:
    TrieNode* root;
    WordDictionary() {
        root = new TrieNode();
    }
    
    void addWord(string word) {
        TrieNode* node = root;
        for (char c: word) {
            if (node -> children[c - 'a'] == nullptr) {
                node -> children[c - 'a'] = new TrieNode();
            }
            node = node -> children[c - 'a'];
        }
        node -> isEnd = true;
    }
    
    bool search(string word) {
        return searchInNode(word, 0, root);
    }

    bool searchInNode(string word, int index, TrieNode* node) {
        if (index == word.size()) return node -> isEnd;
        char c = word[index];
        if (c != '.') {
            if (node -> children[c - 'a'] != nullptr && searchInNode(word, index + 1, node -> children[c - 'a'])) {
                return true;
            }
        } else {
            for (int i = 0; i < 26; i++) {
                if (node -> children[i] != nullptr && searchInNode(word, index + 1, node -> children[i])) {
                    return true;
                }
            }
        }
        return false;
    }
};

  • 18
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值