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.
For 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.
链接: http://leetcode.com/problems/add-and-search-word-data-structure-design/
题解:
设计一个Data Structure来search和add单词。这道题我们又可以用一个R-Way Trie来完成。 像JQuery里面的Auto-complete功能其实就可以用R-Way Trie based method来设计和编程。注意当字符为"."的时候我们要loop当前节点的全部26个子节点,这里要用一个DFS。
Time Complexity - O(n), Space Complextiy - O(26n)。
public class WordDictionary {
private TrieNode root = new TrieNode();
private class TrieNode {
private final int R = 26; // radix = 26
public TrieNode[] next;
public boolean isWord;
public TrieNode() {
next = new TrieNode[R];
}
}
// Adds a word into the data structure.
public void addWord(String word) {
if(word == null || word.length() == 0)
return;
TrieNode node = root;
int d = 0;
while(d < word.length()) {
char c = word.charAt(d);
if(node.next[c - 'a'] == null)
node.next[c - 'a'] = new TrieNode();
node = node.next[c - 'a'];
d++;
}
node.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) {
if(word == null || word.length() == 0)
return false;
TrieNode node = root;
int d = 0;
return search(node, word, 0);
}
private boolean search(TrieNode node, String word, int d) {
if(node == null)
return false;
if(d == word.length())
return node.isWord;
char c = word.charAt(d);
if(c == '.') {
for(TrieNode child : node.next) {
if(child != null && search(child, word, d + 1))
return true;
}
return false;
} else {
return search(node.next[c - 'a'], word, d + 1);
}
}
}
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
二刷:
方法和一刷一样,主要使用Trie。addWord的时候还是使用和Trie的insert一样的的代码。 Search的时候因为有一个通配符'.',所以我们要用dfs搜索节点的26个子节点。
假如使用Python的话可以不用Trie,直接用dict来做。
Java:
Time Complexity: addWord - O(L) , search - O(26L), Space Complexity - O(26L) 这里 L是单词的平均长度。
public class WordDictionary {
TrieNode root = new TrieNode();
// Adds a word into the data structure.
public void addWord(String word) {
if (word == null) return;
TrieNode node = this.root;
int d = 0;
while (d < word.length()) {
int index = word.charAt(d) - 'a';
if (node.next[index] == null) node.next[index] = new TrieNode();
node = node.next[index];
d++;
}
node.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 search(word, root, 0);
}
private boolean search(String word, TrieNode node, int depth) {
if (node == null) return false;
if (depth == word.length()) return node.isWord;
char c = word.charAt(depth);
if (c != '.') {
return search(word, node.next[c - 'a'], depth + 1);
} else {
for (TrieNode nextNode : node.next) {
if (search(word, nextNode, depth + 1)) return true;
}
return false;
}
}
private class TrieNode {
TrieNode[] next;
int R = 26;
boolean isWord;
public TrieNode() {
this.next = new TrieNode[R];
}
}
}
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
Reference:
https://leetcode.com/discuss/35878/java-hashmap-backed-trie
https://leetcode.com/discuss/35928/my-simple-and-clean-java-code
https://leetcode.com/problems/implement-trie-prefix-tree/
https://leetcode.com/discuss/69963/python-168ms-beat-100%25-solution
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 ...
- 【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添加查找单词 - 数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- [leetcode trie]211. Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
随机推荐
- OpenJudge 2721 忽略大小写比较字符串大小
1.Link: http://bailian.openjudge.cn/practice/2721/ 2.Content: 总时间限制: 1000ms 内存限制: 65536kB 描述 一般我们用st ...
- IDEA 2016.2.2激活地址(2016-08-22)
http://idea.imsxm.com/ 2016/10/13 http://idea.iteblog.com/key.php
- [C#]AccessUtils
关键代码: using System; using System.Data; using System.Data.OleDb; namespace CSharpUtilHelpV2 { /// < ...
- Winform DataGridView单元格的提示信息
往往当单元格的内容过多时,显示会变成这样 后缀多了几个点来显示数据未完,当鼠标移到某个单元格时,单元格里的内容会全部显示. 今天偶然发现了一个可以修改提示信息的方法,所以先记下来. 这个方法,可以对于 ...
- php+mysql非缓冲查询(如何循环大数组)
另外一种PHP查询模式是非缓冲查询,数据库服务器会一条一条的返回数据,而不是一次全部返回,这样的结果就是PHP程序消耗较少的内存,但却增加了数据库服务器的压力,因为数据库会一直等待PHP来取数据,一直 ...
- 利用Google GCM发送push通知到Android客户端
// 这个可以需要在google账号中申请,勾选gcm服务选项 $apiKey = 'AIzaSyC6h3ysrn2HDCBqONTo2vKIVVuktIFoxxx'; $headers = arra ...
- htmlcleaner
String xpath = "//div"; Object[] myNodes = node.evaluateXPath(xpath); for (Object obj : my ...
- Spark小课堂Week5 Scala初探
Spark小课堂Week5 Scala初探 Scala是java威力加强版. 对Java的改进 这里会结合StreamingContext.scala这个代码说明下对Java的改进方面. 方便测试方式 ...
- IEtester不靠谱
对于刚刚学习前端的人来说,IEtester无疑是个测试神器, 刚开始用的时候,真有种如获至宝的兴奋. 然而,随着你学习的深入,你会慢慢地发现这个东西不太靠谱,而且会觉得没必要用它.为什么这么说呢? 首 ...
- css3 旋转效果加上双面显示效果
在学习Css3的过程中,我想做一个类似金字塔,菱形翻页效果,如图这种效果 如是,我自己设计了一个,不带js的旋转效果: 1>第一步我先设计了一个方块,内含一个旋转了45deg的小方块,代码如下: ...