[LC] 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
class WordDictionary {
private TrieNode root; /** Initialize your data structure here. */
public WordDictionary() {
root = new TrieNode();
} /** Adds a word into the data structure. */
public void addWord(String word) {
TrieNode cur = root;
for (char c: word.toCharArray()) {
if (cur.children[c - 'a'] == null) {
cur.children[c - 'a'] = new TrieNode();
}
cur = cur.children[c - 'a'];
}
cur.isWord = true;
} /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
public boolean search(String word) {
return helper(word, root, 0);
} private boolean helper(String word, TrieNode root, int level) {
if (level == word.length()) {
return root.isWord;
}
char cur = word.charAt(level);
if (cur == '.') {
for (TrieNode child : root.children) {
if (child != null && helper(word, child, level + 1)) {
return true;
}
}
return false;
} else {
return root.children[cur - 'a'] != null && helper(word, root.children[cur - 'a'], level + 1);
}
}
} class TrieNode {
TrieNode[] children;
boolean isWord; public TrieNode() {
children = new TrieNode[26];
isWord = false;
}
} /**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/
[LC] 211. Add and Search Word - Data structure design的更多相关文章
- 字典树(查找树) 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
Add and Search Word - Data structure design Design a data structure that supports the following two ...
- (*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 ...
- 211. Add and Search Word - Data structure design
题目: Design a data structure that supports the following two operations: void addWord(word) bool sear ...
- 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...
- 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 ...
- 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 Trie树
题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...
随机推荐
- 修改Oracle数据库的字符集
2019年6月,来到了新的环境工作,接触了新的项目. 新的项目需要用Oracle数据库,虽然以前没用过,但是边学边用呗,在安装Oracle数据库的时候完全没有注意到要选择UTF-8的字符集,直接就默认 ...
- JavaScript数组打平(4种方法)
let arr = [1, 2, [3, 4, 5, [6, 7, 8], 9], 10, [11, 12]]; flatten1 = arr => arr.flat(Infinity) fla ...
- 到头来还是逃不过Java - 流程控制
流程控制 没有特殊说明,我的所有这个系列的Java13的笔记都是从廖老师那里摘抄.总结过来的,侵删 引言 兜兜转转到了大四,学过了C,C++,C#,Java,Python,学一门丢一门,到了最后还是要 ...
- PAT Advanced 1033 To Fill or Not to Fill (25) [贪⼼算法]
题目 With highways available, driving a car from Hangzhou to any other city is easy. But since the tan ...
- 负载均衡配置篇(Nginx)
负载均衡 == 分身的能力. 既然要有分身的能力嘛,这好办,多弄几台服务器就搞定了.今天我们讲的实例嘛…..我们还是先看图比较好: 还是图比较清晰,以下我都用别名称呼: PA : 负载均衡服务器/WE ...
- python os.path.dirname() abspath()
测试文件的名称 path_test.py 先确定文件目录 (my_flask3) python@ubuntu:~/Desktop/flask_news_pro$ python path_test.py ...
- java使用forEach填充字典值
// 填充字典值 Vector vector = vectorMapper.selectByPrimaryKey(id); VectorModel vectorModel = new VectorMo ...
- Linux进程的诞生和消亡
1.进程的诞生 (1).进程0和进程1 (内核里边的固有的) (2).fork函数和vfork函数用于新进程的产生 2.进程的消亡 (1).正常终止和异常终止 (2).进程在运行时需要消耗系统资源(内 ...
- 六、Shell脚本高级编程实战第六部
一.写一个start_nginx脚本,当启动.停止.重启时利用系统函数模拟实现系统脚本启动的特殊颜色效果 (用if实现) #!/bin/sh. /etc/init.d/functions if [ $ ...
- 理解ClassLoader工作机制
package com.ioc; public class Test { public static void main(String[] args) throws ClassNotFoundExce ...