​LeetCode刷题实战211:添加与搜索单词

算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !

今天和大家聊的问题叫做 添加与搜索单词,我们先来看题面:

https://leetcode-cn.com/problems/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.

题意

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

实现词典类 WordDictionary :

  • WordDictionary() 初始化词典对象

  • void addWord(word) 将 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"); // return False
wordDictionary.search("bad"); // return True
wordDictionary.search(".ad"); // return True
wordDictionary.search("b.."); // return True
 

提示:
1 <= word.length <= 500
addWord 中的 word 由小写英文字母组成
search 中的 word 由 '.' 或小写英文字母组成
最多调用 50000 次 addWord 和 search

解题

用字典树来作为存储的数据结构。

新增单词的时候,就使用字典树插入新单词的方法。

在查找某一个字典树时,使用深度优先搜索即可。

class WordDictionary {
public:
    //定义字典树中每个节点的结构
    struct Node{
        bool isword = false; //用于标记当前节点是否为单词的最后一个字符
        Node* next[27] = {};
    };
    Node* root; //每一个class都有一棵字典树
    /** Initialize your data structure here. */
    WordDictionary() {
        root = new Node(); //新建一棵字典树
    }
    
    /** Adds a word into the data structure. */
    void addWord(string word) {
        Node* tmp = root;
        for(auto w: word){
            if(tmp->next[w - 'a'] == NULL){ //还没有这个节点
                Node* tt = new Node();
                tmp->next[w - 'a'] = tt; //那就新建节点
            }
            tmp = tmp->next[w - 'a'];
        }
        tmp->isword = true; //遍历完一个单词
    }
    
    /** 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 dfs(word, root, 0);
    }
    
    bool dfs(string word, Node* root, int i){
        if(!root) return false;
        if(i >= word.size()) return root->isword; //看看是不是一个完整的单词
        if(word[i] != '.'){
            if(root->next[word[i] - 'a']){
                return dfs(word, root->next[word[i] - 'a'], i+1);
            }
            else return false;
        }
        for(auto t: root->next){
            if(t){
                if(dfs(word, t, i+1)) return true;
            }
        }
        return false;
    }
};

好了,今天的文章就到这里,如果觉得有所收获,请顺手点个在看或者转发吧,你们的支持是我最大的动力 。

上期推文:

LeetCode1-200题汇总,希望对你有点帮助!

LeetCode刷题实战201:数字范围按位与

LeetCode刷题实战202:快乐数

LeetCode刷题实战203:移除链表元素

LeetCode刷题实战204:计数质数

LeetCode刷题实战205:同构字符串

LeetCode刷题实战206:反转链表

LeetCode刷题实战207:课程表

LeetCode刷题实战208:实现 Trie (前缀树)

LeetCode刷题实战209:长度最小的子数组

LeetCode刷题实战210:课程表 II

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员小猿666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值