[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查找就可 ...
随机推荐
- SeetaFaceEngine系列2:Face Alignment编译和使用
前面一篇写了编译人脸检测部分,现在就介绍下人脸配准部分,SeetaFace的Face Alignment通过人脸的五个关键点来配准人脸,也就是双眼.鼻尖.两个嘴角. 这部分的编译也和上一篇一样,步骤如 ...
- LeetCode Input Initial Code
说明 LeetCode提供的样本输入,显示上是数组Array,而后台的实际测试用例则是树TreeNode,链表ListNode等. 如果你是在页面手撸代码直接提交的,那没什么影响. 如果你是在本地ID ...
- build模式入门,build模式理解(转载)
问:为何要用? 普通做法: 1.创建pojo public class Person { private String name; private int age; private double he ...
- Codeforces 1288C - Two Arrays
题目大意: 给定n和m,有两个数组,两个数组的长度都等于m 数组内每个元素都在1到n中 对于两个数组对应的位置i,必须满足a[i]<=b[i] a数组必须是不下降的序列 b数组必须是不上升的序列 ...
- Maven--传递性依赖和依赖范围
依赖范围不仅可以控制依赖与三种 classpath 的关系,还对传递性依赖产生影响. 假设 A 依赖于 B,B依赖于 C,我们说 A 对于 B 是第一直接依赖,B 对于 C 是第二直接依赖,A 对于 ...
- altium designer 画板 电子元器件的名称不能集体修改
今天在画板子的时候,遇到一个问题,就是发现电子元器件的名字太大了,想把他们集体都改小一点,方便布局,结果修改的时候却发现高度可以改,宽度改不了,(集体改的话,改不了,但是可以单独改,只是单独改,工程量 ...
- 《C程序设计(第四版)》小记
我看的这本书很经典,它是谭浩强写的,也就是广为流传的“C语言红皮书”.在网上看了很多帖子,生活中也问过一些朋友,大多数人是不认可这本书的.很多人都说这本书很烂,看不懂,然后去“追逐”国外的一些教材.其 ...
- redhat6.5 升级内核
1.导入key rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org 2.安装elrepo的yum源 rpm -Uvh https:// ...
- linux xargs详解
xargs [-0prtx] [-E eof-str] [-e[eof-str]] [--eof[=eof-str]] [--null] [-d delimiter] [--delimiter d ...
- iOS 加急审核的办法
前言:由于自己的APP在提交后,审核了大概一周左右还没有消息,而领导又不断询问情况,于是自己在网上看到了这篇文章.由于自己比较懒,所以在此记录下来,以供 大家参考. 说明:本文只是做一个记录,还望看到 ...