【刷题-LeetCode】211. Add and Search Word - Data structure design
- 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
.
解法1 hashmap。将单词按照长度映射
class WordDictionary {
public:
unordered_map<int, set<string>>mp;
/** Initialize your data structure here. */
WordDictionary() { }
/** Adds a word into the data structure. */
void addWord(string word) {
int m = word.size();
mp[m].insert(word);
}
/** 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) {
int m = word.size();
if(mp.count(m) == 0)return false;
for(auto s : mp[m]){
int i;
for(i = 0; i < m; ++i){
if(s[i] == word[i] || word[i] == '.')continue;
else break;
}
if(i == m)return true;
}
return false;
}
};
解法2 trie-tree。查找时,对于包含了.
的部分,递归往后查询
class trie_node{
public:
bool isWord;
vector<trie_node*>child;
trie_node() : isWord(false), child(26, NULL){}
~trie_node(){
for(auto &c : child)delete c;
}
};
class WordDictionary {
public:
trie_node* root;
/** Initialize your data structure here. */
WordDictionary() {
root = new trie_node;
}
/** Adds a word into the data structure. */
void addWord(string s) {
trie_node *cur = root;
for(int i = 0; i < s.size(); ++i){
int idx = s[i] - 'a';
if(cur->child[idx] == NULL){
cur->child[idx] = new trie_node();
}
cur = cur->child[idx];
}
cur->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 _search(word, root);
}
bool _search(string s, trie_node* root){
if(s == "")return root->isWord;
if(s[0] == '.'){
for(int j = 0; j < 26; ++j){
if(root->child[j] && _search(s.substr(1), root->child[j]))return true;
}
return false;
}else{
if(root->child[s[0]-'a'] == NULL)return false;
return _search(s.substr(1), root->child[s[0]-'a']);
}
}
};
【刷题-LeetCode】211. Add and Search Word - Data structure design的更多相关文章
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- (*medium)LeetCode 211.Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- leetcode@ [211] Add and Search Word - Data structure design
https://leetcode.com/problems/add-and-search-word-data-structure-design/ 本题是在Trie树进行dfs+backtracking ...
- [LeetCode] 211. Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- leetcode 211. Add and Search Word - Data structure design Trie树
题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...
- [leetcode]211. Add and Search Word - Data structure design添加查找单词 - 数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design
字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...
- 【LeetCode】211. Add and Search Word - Data structure design
Add and Search Word - Data structure design Design a data structure that supports the following two ...
- 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...
随机推荐
- CF1440A Buy the String 题解
Content 有 \(t\) 组询问,每组询问给出一个长度为 \(n\) 的 \(0/1\) 串,你可以花 \(h\) 的代价把 \(0\) 修改成 \(1\) 或者把 \(1\) 修改成 \(0\ ...
- Photoshop学习笔记(一)
1.Alt+delete,用前景色填充选区 2.按住shift键可以新加选区 3.按住alt键可以减去选区 4.第一次选择选区时按住shift键制作出正方形或者圆形 5.第一次选择选区时按住alt键将 ...
- MyBatis学习(五)MyBatis-开启log4j日志
1.前言 Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件,甚至是套接口服务器.NT的事件记录器.UNIX Syslog守护进程等 ...
- JS中使用reconnecting-websocket实现websocket断开自动重新连接
这里用了第三方的js 官方地址:https://github.com/joewalnes/reconnecting-websocket 引入js reconnecting-websocket.min. ...
- Swagger请求报错:TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.
TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body. 如下图 ...
- linux 设备文件的操作
文件:包含数据,具有属性,通过目录中的名字被标识, 可以从文件读数据,可以向文件写数据. 设备也支持文件的操作. 每个设备都被当作一个文件,具有文件名,i-节点号,文件所有者,权限位的集合,最新修改时 ...
- 【剑指Offer】10- II. 青蛙跳台阶问题 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人微信公众号:负雪明烛 目录 题目描述 解题方法 动态规划 日期 题目地址:https: ...
- PlatformIO+Jlink进行调试
PlatformIO自带调试功能具体配置如下 https://docs.platformio.org/en/latest/plus/debug-tools/jlink.html 我是用的是直接 ...
- [opencv]GeneralProcessing_Template_Function
// // Created by leoxae on 2019-05-08. // #ifndef OPENCVDEMO_UTILS_H #define OPENCVDEMO_UTILS_H #inc ...
- 搞一下vue生态,从vuex开始
Vuex vuex 是专门帮助vue管理的一个js库,利用了vue.js中细粒度数据响应机制来进行高效的状态更新. vuex核心就是store,store就是个仓库,这里采用了单一的store状态树, ...