Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >LeetCode 0211 - Add and Search Word - Data structure design

LeetCode 0211 - Add and Search Word - Data structure design

作者头像
Reck Zhang
发布于 2021-08-11 04:08:42
发布于 2021-08-11 04:08:42
23900
代码可运行
举报
文章被收录于专栏:Reck ZhangReck Zhang
运行总次数:0
代码可运行

Add and Search Word - Data structure design

Desicription

Design a data structure that supports the following two operations:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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.

Solution

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary obj = new WordDictionary();
 * obj.addWord(word);
 * bool param_2 = obj.search(word);
 */
class WordDictionary {
private:
    class TrieNode {
    public:
        bool isWord = false;
        TrieNode* children[26] = {nullptr};
    };
    TrieNode* root = new TrieNode();
public:
    /** Initialize your data structure here. */
    WordDictionary() = default;

    /** Adds a word into the data structure. */
    void addWord(string word) {
        TrieNode* run = root;
        for(char c : word) {
            if(nullptr == run->children[c - 'a'])
                run->children[c - 'a'] = new TrieNode();
            run = run->children[c - 'a'];
        }
        run->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 query(std::move(word), root);
    }
    bool query(string word, TrieNode* run, int index = 0) {
        for(int i = index; word[i]; i++) {
            if(nullptr != run && word[i] != '.')
                run = run->children[word[i] - 'a'];
            else if(nullptr != run && word[i] == '.') {
                TrieNode* tmp = run;
                for(char c = 'a'; c <= 'z'; c++) {
                    word[i] = c;
                    run = tmp->children[word[i] - 'a'];
                    if(query(word, run, i+1))
                        return true;
                }
            }
            else break;
        }
        return run && run->isWord;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-06-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Trie - 211. Add and Search Word - Data structure design
211. Add and Search Word - Data structure design Design a data structure that supports the following two operations:
ppxai
2020/09/23
2640
Leetcode 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.
triplebee
2018/01/12
5730
Leetcode 211: Add and Search Word - Data structure design
https://blog.baozitraining.org/2019/03/leetcode-solution-211-add-and-search.html
包子面试培训
2019/04/30
7330
golang刷leetcode 字符串(5)添加与搜索单词 - 数据结构设计
search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z 。. 可以表示任何一个字母。
golangLeetcode
2022/08/02
1700
添加与搜索单词 - 数据结构设计
请你设计一个数据结构,支持 添加新单词 和 查找字符串是否与任何先前添加的字符串匹配 。
狼啸风云
2023/12/03
2080
LeetCode 211. 添加与搜索单词 - 数据结构设计(Trie树)
void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z 。 . 可以表示任何一个字母。
Michael阿明
2020/07/13
4210
LeetCode 211. 添加与搜索单词 - 数据结构设计(Trie树)
LeetCode 211. Add and Search Word - Data structure design(字典树)
字典树。 class WordDictionary { public: int map[100005][26]; int tag[100005]; int num; /** Initialize your data structure here. */ WordDictionary() { memset(map,0,sizeof(map)); memset(tag,0,sizeof(tag)); num
ShenduCC
2020/02/19
4040
LeetCode 211.添加与搜索单词(数据结构设计) - JavaScript
search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z 。 . 可以表示任何一个字母。
心谭博客
2020/04/21
4620
字典树概念与题型解析
这次讲一个不经常被人提起的数据结构 - 字典树,虽说知名度不高,但是这个数据结构可以解决其他数据结构所不能解决,或者是比较难解决的问题,而且性能方面,相对于其他的功能类似的数据结构会更优,文章会从概念与基本实现,性能分析,题型解析三大方向来介绍字典树。
五分钟学算法
2019/10/18
6160
【设计数据结构】Trie 运用题
这是 LeetCode 上的「211. 添加与搜索单词 - 数据结构设计」,难度为「中等」。
宫水三叶的刷题日记
2022/04/07
5090
【设计数据结构】Trie 运用题
Leetcode 212 Word Search II 字典树 + 回溯
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell
triplebee
2018/01/12
5420
LeetCode 0208 - Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods.
Reck Zhang
2021/08/11
2560
数据结构之Trie字典树
Trie 树,也叫“字典树”或“前缀树”。顾名思义,它是一个树形结构。但与二分搜索树、红黑树等不同的是,Trie 树是一种多叉树,即每个节点可以有 m 个子节点。它是一种专门处理字符串匹配的数据结构,用来解决在一组字符串集合中快速查找某个字符串的问题。
端碗吹水
2021/01/29
8570
算法细节系列(23):回溯
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/72655010
用户1147447
2019/05/26
6140
☆打卡算法☆LeetCode 211. 添加与搜索单词 - 数据结构设计 算法解析
大家好,我是小魔龙,Unity3D软件工程师,VR、AR,虚拟仿真方向,不定时更新软件开发技巧,生活感悟,觉得有用记得一键三连哦。
恬静的小魔龙
2022/09/27
2450
☆打卡算法☆LeetCode 211. 添加与搜索单词 - 数据结构设计  算法解析
Trie - 208. Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods.
ppxai
2020/09/23
5560
【图解算法】模板+变式——带你彻底搞懂字典树(Trie树)
接下来将对经典的字典树进行代码实现;接着做几个变体题目深入理解字典树的强大;最后回到日常生活,瞧瞧字典树怎样融入到了我们的生活之中 >_<
全栈程序员站长
2022/10/04
1.4K0
【图解算法】模板+变式——带你彻底搞懂字典树(Trie树)
LeetCode 0212 - Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board.
Reck Zhang
2021/08/11
2000
Leetcode 208 solution: Implement Trie (Prefix Tree)
https://blog.baozitraining.org/2019/04/leetcode-solution-208-implement-trie.html
包子面试培训
2019/04/30
6250
Leetcode 题目解析:211. 添加与搜索单词 - 数据结构设计
在考察算法题时,我们往往离不开数据结构。而常见和常用的数据结构,以堆、栈、单/双链表、HashMap、各种二叉树(二叉树、平衡二叉树、搜索二叉树、红黑树)最为常见。另外,像bitmap等也比较多,尤其是需要位操作的时候。但还有一些数据结构也会占有一席之地,例如树中的Trie树(字典树),在检索类题目中也非常常见。
程序员架构进阶
2021/11/04
6400
相关推荐
Trie - 211. Add and Search Word - Data structure design
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验