LeetCode 211 - Add and Search Word - Data structure design

一、问题描述

Description:

Design a data structure that supports the following two operations:

void addWord(word)
bool search(word)

search(word) can search a literal word or a regular expression string containing only letters a-z or. and . means it can represent any one letter.

For example:

addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -> false
search("bad") -> true
search(".ad") -> true
search("b..") -> true

Note:
You may assume that all words are consist of lowercase letters a-z.

设计一个数据结构,支持两种操作:

void addWord(word)
bool search(word)

search 操作可以查找字符串常量,或者一个包含.的正则表达式。其中.可以表示任何单个的字符。

假定所有的单词都是由小写字母组成。


二、解题报告

要实现一个具有字符串检索功能的词典,我们当然会想到 Trie树。请看《Trie树的应用与实现》与《LeetCode 208-Implement Trie (Prefix Tree)》。

  1. 当要插入一个字符串时,将它插入到 Trie 树即可。
  2. 当要检索一个字符串时,若该字符串含有.,由于.可以匹配当前层的所有节点,这时需要同时向下检索多条路径(使用递归)。

solution代码如下:

/*********Trie树的节点结构********/
class TrieNode {
public:
    bool iskey;   // 标记该节点是否代表关键字
    TrieNode *children[26]; // 各个子节点
    TrieNode() {
        iskey = false;
        for(int i=0; i<26; ++i)
            children[i] = NULL;
    }
};

/***************词典***************/
class WordDictionary {
public:
    WordDictionary() {
        root = new TrieNode();
    }

    // 插入一个单词
    void addWord(string word) {
        TrieNode* node = root;
        for(int i=0; i<word.size(); ++i)
        {
            if(node->children[word[i]-'a'] == NULL)
            {
                node->children[word[i]-'a'] = new TrieNode();
            }
            node = node->children[word[i]-'a'];
        }
        node->iskey = true;
    }

    // 检索单词,可以包含'.'
    bool search(string word) {
        return searchWord(word, root, 0);
    }

private:
    TrieNode* root;

    bool searchWord(string &word, TrieNode* node, int p) {
        if(p == word.size())
            return node->iskey;

        if(word[p] == '.') {    // 匹配任何字符
            for(int i=0; i<26; ++i)
                if(node->children[i] && searchWord(word, node->children[i], p+1))
                    return true;
            return false;
        }
        else
            return node->children[word[p]-'a'] && searchWord(word, node->children[word[p]-'a'], p+1);
    }
};





LeetCode答案源代码:https://github.com/SongLee24/LeetCode


转载于:https://www.cnblogs.com/songlee/p/5738058.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值