【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);
 */
相关推荐
努力学算法的蒟蒻6 小时前
day79(2.7)——leetcode面试经典150
算法·leetcode·职场和发展
2401_841495646 小时前
【LeetCode刷题】二叉树的层序遍历
数据结构·python·算法·leetcode·二叉树··队列
2401_841495646 小时前
【LeetCode刷题】二叉树的直径
数据结构·python·算法·leetcode·二叉树··递归
我是咸鱼不闲呀6 小时前
力扣Hot100系列19(Java)——[动态规划]总结(上)(爬楼梯,杨辉三角,打家劫舍,完全平方数,零钱兑换)
java·leetcode·动态规划
铉铉这波能秀7 小时前
LeetCode Hot100数据结构背景知识之列表(List)Python2026新版
数据结构·leetcode·list
仟濹8 小时前
算法打卡 day1 (2026-02-06 周四) | 算法: DFS | 1_卡码网98 可达路径 | 2_力扣797_所有可能的路径
算法·leetcode·深度优先
YuTaoShao9 小时前
【LeetCode 每日一题】1653. 使字符串平衡的最少删除次数——(解法一)前后缀分解
算法·leetcode·职场和发展
VT.馒头9 小时前
【力扣】2727. 判断对象是否为空
javascript·数据结构·算法·leetcode·职场和发展
老鼠只爱大米11 小时前
LeetCode经典算法面试题 #46:全排列(回溯、交换、剪枝等五种实现方案详细解析)
算法·leetcode·剪枝·回溯·全排列·stj算法
im_AMBER12 小时前
Leetcode 114 链表中的下一个更大节点 | 删除排序链表中的重复元素 II
算法·leetcode