【Leetcode】211. 添加与搜索单词 - 数据结构设计

一、题目

1、题目描述

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

实现词典类 WordDictionary

  • WordDictionary() 初始化词典对象
  • void addWord(word)word 添加到数据结构中,之后可以对它进行匹配
  • bool search(word) 如果数据结构中存在字符串与 word 匹配,则返回 true ;否则,返回 falseword 中可能包含一些 '.' ,每个 . 都可以表示任何一个字母。

示例:

复制代码
输入:
["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"); // 返回 False
wordDictionary.search("bad"); // 返回 True
wordDictionary.search(".ad"); // 返回 True
wordDictionary.search("b.."); // 返回 True

提示:

  • 1 <= word.length <= 25
  • addWord 中的 word 由小写英文字母组成
  • search 中的 word'.' 或小写英文字母组成
  • 最多调用 104addWordsearch

2、基础框架

cpp 复制代码
class WordDictionary {
public:
    WordDictionary() {
        
    }
    
    void addWord(string word) {
        
    }
    
    bool search(string word) {
        
    }
};

/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary* obj = new WordDictionary();
 * obj->addWord(word);
 * bool param_2 = obj->search(word);
 */

3、原题链接

211. 添加与搜索单词 - 数据结构设计

二、解题报告

1、思路分析

主要思路同【Leetcode】208.实现Trie(前缀树),但是需要注意,插入的时候只有小写字母字符,而查找的时候有"."和小写字母字符,所以遇到 "." 字符时,所有子孩子非空的情况都要进行尝试。

2、时间复杂度

3、代码详解

cpp 复制代码
class WordDictionary {
private:
    class Node {
    public:
        bool end;
        Node *childs[26];
        Node() : end(false) {
            memset(childs, 0, sizeof(childs));
        }
    };

    Node *root;
	
	//深度优先搜索
    bool pathSearch(string word, Node *root, int index) {
        if (index == word.size()) {
            return root->end;
        }

        Node *node = root;
        int path = 0;
        
        
        if (word[index] == '.') { //字符.
            for (int i = 0; i < 26; i++) { //所有非空的孩子都要尝试
                if (node->childs[i]) {
                    bool res = pathSearch(word, node->childs[i], index + 1);
                    if (res) return true;
                }
            }
            return false;
        } else { //字母字符
            path = word[index] - 'a';
            if (node->childs[path] == nullptr) {
                return false;
            }
            return pathSearch(word, node->childs[path], index + 1);
        }
        
    }
public:
    WordDictionary() {
        root = new Node();
    }
    
    void addWord(string word) {
        Node *node = root;
        int path = 0;
        for (int i = 0; word[i]; i++) {
            path = word[i] - 'a';
            if (node->childs[path] == nullptr) {
                node->childs[path] = new Node();
            }
            node = node->childs[path];
        }
        node->end = true;
    }


    bool search(string word) {
        return pathSearch(word, root, 0);
    }
};

/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary* obj = new WordDictionary();
 * obj->addWord(word);
 * bool param_2 = obj->search(word);
 */
相关推荐
科大饭桶4 小时前
数据结构自学Day5--链表知识总结
数据结构·算法·leetcode·链表·c
YuTaoShao6 小时前
【LeetCode 热题 100】24. 两两交换链表中的节点——(解法一)迭代+哨兵
java·算法·leetcode·链表
前端拿破轮8 小时前
翻转字符串里的单词,难点不是翻转,而是正则表达式?💩💩💩
算法·leetcode·面试
凤年徐8 小时前
【数据结构与算法】203.移除链表元素(LeetCode)图文详解
c语言·开发语言·数据结构·算法·leetcode·链表·刷题
李昊_8 小时前
【LeetCode 3440. 重新安排会议得到最多空余时间 II】解析
算法·leetcode
呆呆的小鳄鱼9 小时前
leetcode:322. 零钱兑换[完全背包]
算法·leetcode·职场和发展
এ᭄画画的北北9 小时前
力扣-240.搜索二维矩阵 II
算法·leetcode·矩阵
JJ1M810 小时前
前缀和+贪心总结,基于每日一题力扣3439、3440
python·算法·leetcode
呆呆的小鳄鱼10 小时前
leetcode:518. 零钱兑换 II[完全背包]
算法·leetcode·职场和发展
沧澜sincerely11 小时前
二分查找【各种题型+对应LeetCode习题练习】
算法·leetcode·二分查找