Trie - 211. Add and Search Word - Data structure design
时间:2022-07-25
本文章向大家介绍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:
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:
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
.
思路:
由于这一题的匹配是可以直接使用
.
代替任意字符,所以可以用一个map存下左右的输入字符串,然后用简单的单个字符的比较来做search,但是这里使用前缀树来做。
代码:
go:
const R = 26
type WordDictionary struct {
Children [R]*WordDictionary
Word bool
}
/** Initialize your data structure here. */
func Constructor() WordDictionary {
return WordDictionary{}
}
/** Adds a word into the data structure. */
func (this *WordDictionary) AddWord(word string) {
cur := this
for i := 0; i < len(word); i++ {
c := rune(word[i])
if cur.Children[c - 'a'] == nil {
cur.Children[c - 'a'] = &WordDictionary{Children : [R]*WordDictionary{}, Word:false}
}
cur = cur.Children[c - 'a']
}
cur.Word = true
}
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
func (this *WordDictionary) Search(word string) bool {
return searchWord(word, this)
}
func searchWord(word string, node *WordDictionary) bool {
for i := 0; i < len(word) && node != nil; i++ {
c := rune(word[i])
if c != '.' {
node = node.Children[c - 'a']
} else {
temp := node
for _, children := range temp.Children {
node = children
if searchWord(word[i+1:], node) {
return true
}
}
}
}
return node != nil && node.Word
}
/**
* Your WordDictionary object will be instantiated and called as such:
* obj := Constructor();
* obj.AddWord(word);
* param_2 := obj.Search(word);
*/
- python装饰器大详解
- spring boot 登录注册 demo (四) -- 体验小结
- jenkins 时区设置
- 什么样的密码才是安全的?
- MAC本遭遇ARP攻击的处理办法
- nodejs 语法学习(持续更新)
- Django——模板层(template)(模板语法、自定义模板过滤器及标签、模板继承)
- - Templates should only be responsible for mapping the state to the UI. Avoid placing tags with side
- Django - - - -视图层之视图函数(views)
- fiddler mock ==> AutoResponder
- 基于Node.js开发跨平台窗口程序
- Django视图层之路由配置系统(urls)
- java String时间转为时间戳
- linux 简易启动脚本
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法