211. 添加与搜索单词-数据结构设计
- 题号:211
- 来源:LeetCode
- 难度:中等
- 标签:
字典树设计字符串 - 语言:TypeScript
- 解法:1 个
- 作者:lmliheng
- 最近更新:2026-09-27
TypeScript · 更新于 2026-09-27
字典树实现 addWord,search 未处理通配符
typescript
/**
* @difficulty medium
* @tags 字典树,设计,字符串
* @note 字典树实现 addWord,search 未处理通配符
* @211. 添加与搜索单词-数据结构设计
*/
var WordDictionary = function() {
this.children={}
};
/**
* @param {string} word
* @return {void}
*/
WordDictionary.prototype.addWord = function(word: string) {
let node = this.children
for (let i = 0; i < word.length; i++) {
if (!node[word[i]]) {
node[word[i]] = {}
}
node = node[word[i]]
}
node.isEnd = true
};
/**
* @param {string} word
* @return {boolean}
*/
WordDictionary.prototype.search = function(word: string) {
const node = this.searchPrefix(word);
return node !== undefined && node.isEnd !== undefined;
};
WordDictionary.prototype.searchPrefix = function (prefix: string) {
let node = this.children;
for (const ch of prefix) {
}
return node;
}
let wd=new WordDictionary()
wd.addWord('haha')源码:ts/leetcode/211. 添加与搜索单词-数据结构设计.ts
在 GitHub 上查看题目所在目录:lmliheng/algorithm