[WXM] LeetCode 211. Add and Search Word - Data structure design C++

211. Add and Search Word - Data structure design

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 .. A . means it can represent any one letter.

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.

Approach

  1. 题目大意实现字典树的查找和插入的方法。然后与正常的查找有一点点不同,.可以匹配任意字符,那么查找就要用DFS,如何设计DFS是挺关键的,因为设计不好,不仅难理解,而且可能会变得复杂,首先我们对每个字符深搜,不过字符分两种情况,一个是非.,一个是.,我先讨论第一种情况,因为是非.,所以字符是确定好的了,我继续往下搜即可,第二种情况,因为是.,字符是不确定的,我们要枚举26种可能,直到有一个可以为止。递归边界很明显是最后一个字符,我们只要判断该字符是不是某个单词结尾即可,细节等部分看代码会更好。

Code

struct Trie
{
    Trie* letter[26];
    bool isword;
    Trie() {
        isword = false;
        for (int i = 0; i < 26; i++) {
            letter[i] = nullptr;
        }
    }
};
class WordDictionary {
public:
    Trie *root;
    /** Initialize your data structure here. */
    WordDictionary() {
        root = new Trie();
    }

    /** Adds a word into the data structure. */
    void addWord(string word) {
        Trie *head = root;
        for (char &w : word) {
            int c = w - 'a';
            if (!head->letter[c]) {
                head->letter[c] = new Trie();
            }
            head = head->letter[c];
        }
        head->isword = true;
    }
    bool Search_str(string &word, Trie *head, int n, int pos) {
        if (pos == n) {
            if (word[pos] == '.') {
                for (int i = 0; i < 26; i++) {
                    if (!head->letter[i])continue;
                    if (head->letter[i]->isword)return true;
                }
                return false;
            }
            return head&&head->letter[word[pos] - 'a'] && head->letter[word[pos] - 'a']->isword;
        }
        if (word[pos] != '.') {
            if (!head->letter[word[pos] - 'a'])return false;
            return Search_str(word, head->letter[word[pos] - 'a'], n, pos + 1);
        }
        for (int i = 0; i < 26; i++) {
            if (!head->letter[i])continue;
            if (Search_str(word, head->letter[i], n, pos + 1)) {
                return true;
            }
        }
        return false;
    }
    /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
    bool search(string word) {
        return Search_str(word, root, word.size() - 1, 0);
    }
};
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* bool param_2 = obj.search(word);
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值