1. 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的更多相关文章

  1. 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 ...

  2. (*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 ...

  3. leetcode@ [211] Add and Search Word - Data structure design

    https://leetcode.com/problems/add-and-search-word-data-structure-design/ 本题是在Trie树进行dfs+backtracking ...

  4. [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 ...

  5. leetcode 211. Add and Search Word - Data structure design Trie树

    题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...

  6. [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 ...

  7. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

  8. 【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 ...

  9. 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...

随机推荐

  1. AJAX get和post请求

    <!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>&l ...

  2. 使用xlsx实现Excel导入

    需求 实现在系统里批量导入数据,通过上传一个excel文件,前端将文件处理为json数据发送给后端.(最好与后端定义好上传的文件模板,方便处理数据) 实现 使用xlsx: xlsx的github地址: ...

  3. centos使用docker安装clickhouse

    拉取镜像 docker pull yandex/clickhouse-server:20.3.12.112 启动 docker run -d --name=clickhouse-server -p 8 ...

  4. CountDownLatch源码阅读

    简介 CountDownLatch是JUC提供的一个线程同步工具,主要功能就是协调多个线程之间的同步,或者说实现线程之间的通信 CountDown,数数字,只能往下数.Latch,门闩.光看名字就能明 ...

  5. 【九度OJ】题目1174:查找第K小数 解题报告

    [九度OJ]题目1174:查找第K小数 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1174 题目描述: 查找一个数组的第 ...

  6. 【LeetCode】606. Construct String from Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 日期 题目地址:https://l ...

  7. 【LeetCode】921. Minimum Add to Make Parentheses Valid 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...

  8. BST的中序后继

    二叉搜索树中的顺序后继:从BST中找到指定节点的下一个节点. 比如1的下一个是2,2的下一个是3,4的下一个是5. 思路: 方法1:递归执行中序遍历,获取list,得到p的下一个.时间O(N),空间O ...

  9. 1298 - One Theorem, One Year

    1298 - One Theorem, One Year   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

  10. 2019HPU-ICPC-Training-1

    byl太强了,学弟们太强了-全程被吊打,嘤嘤嘤- A题  Connecting Vertices http://codeforces.com/problemset/problem/888/F 不会 B ...