Leetcode-Trie

本文介绍了一种数据结构的设计方法,该结构支持添加单词(addWord)和搜索功能(search),其中包括精确匹配和使用通配符(.)进行模糊匹配。通过使用自定义的哈希节点结构实现了高效的查找。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

For example:

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

#include <string>
#include <iomanip>
#include <utility> 
#include <memory>
#include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>



using namespace std;



template<typename T>
class nodeHash {
public:
    nodeHash():len(0){}
    nodeHash(const T&t): val(t) {}
    T val;
    vector<shared_ptr<nodeHash<T>>> children;
    unordered_set<char> endNode;
    unordered_map<T, shared_ptr<nodeHash<T>>> mmps;
    size_t len;
};

class WordDictionary{
public:
    WordDictionary():root(nullptr){}
    shared_ptr<nodeHash<char>> root;
    void addWord(const string& word) {
        if (!root) root = make_shared<nodeHash<char>>('\0');
        auto it = root;
        for (size_t i = 0; i < word.size(); ++i) {
            if (i+1 == word.size()) {
                it->endNode.insert(word[i]);
            }
            auto resf = it->mmps.find(word[i]);
            if (resf != it->mmps.end()) {
                it = resf->second;
            } else {
                shared_ptr<nodeHash<char>> tmpptr = make_shared<nodeHash<char>>(nodeHash<char>(word[i]));
                it->children.push_back(tmpptr);
                pair<char, decltype(tmpptr)> tmppair;
                tmppair.first = word[i];
                tmppair.second = tmpptr;
                it->mmps.insert(tmppair);
                it = tmpptr;
            }
        }
        return;
    }
    bool search(const string &word,size_t index ,decltype(root) it) { /*dsf*/
        if (index+1 == word.size()) {
            if (!it)return false;
            char c = word.back();
            if (c != '.') {
            auto resf = it->endNode.find(word.back());
            if (resf != it->endNode.end()) return true;
            else return false;
            }
            else {
                if (!(it->endNode).empty())return true;
                else return false;
            }
        }
        else if ( ( (it->mmps).empty() && index < word.size())

                 ||  
                ( (!(it->mmps).empty()) && index >= word.size())

                 )
                 {
            return false;
        }

        if (index >= word.size()) return true;
        if (!it) return false;
        char c = word[index];
        if (c != '.') {
            auto resf = it->mmps.find(c);
            if (resf == it->mmps.end()) {
                return false;
            } else {
                it = resf->second;
                return search(word, index+1, it);
            }

        } else {
            for (auto &c1 : it->mmps) {
                if (search(word, index+1, c1.second))
                    return true;
            }
            return false;
        }

    }

    bool search(string word) {
        if ((!root) || (root->mmps).empty()) return false;
        auto it = root;
        return search(word, 0, it);
    }
};
/**
 * 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、付费专栏及课程。

余额充值