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.

click to show hint.

You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first.
方法:1:暴力解法,超时
代码如下:
public class WordDictionary {

    private List<String>list=new ArrayList<>();
// Adds a word into the data structure.
public void addWord(String word) {
list.add(word);
} // 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(list.contains(word)){
return true;
}else{
int size=list.size();
int i=0,j=0;
for(;i<size;i++){
String s= list.get(i);
int len=s.length();
if(len!=word.length())
continue;
j=0;
for(;j<len;j++){
if(word.charAt(j)=='.')
continue;
else{
if(s.charAt(j)!=word.charAt(j)){
break;
}
}
}
if(j==len) return true;
}
return false;
}
}
} // Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");

运行结果:

方法2:按照提示,使用单词查字树

代码如下:

public class WordDictionary {

    public class TrieNode{
public TrieNode[] children=new TrieNode[26];
public String item="";
}
private TrieNode root=new TrieNode();
// Adds a word into the data structure.
public void addWord(String word) {
TrieNode node=root;
for(char c:word.toCharArray()){
if(node.children[c-'a']==null){
node.children[c-'a']=new TrieNode();
}
node=node.children[c-'a'];
}
node.item=word;
} // 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 match(word.toCharArray(),0,root);
}
private boolean match(char[] chs,int k,TrieNode node){
if(k==chs.length) return !node.item.equals("");
if(chs[k]!='.'){
return node.children[chs[k]-'a']!=null && match(chs,k+1,node.children[chs[k]-'a']);
}else{
for(int i=0;i<node.children.length;i++){
if(node.children[i]!=null){
if(match(chs,k+1,node.children[i])){
return true;
}
}
}
}
return false;
}
} // Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("patter");
 运行结果:

(*medium)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. leetcode@ [211] Add and Search Word - Data structure design

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

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

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

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

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

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

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

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

  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. 解决IE11出现异常SCRIPT5011:不能执行已释放Script的代码

    功能概述: 最近做了一个教育科研系统,由于时间比较紧,所以能集成的功能都尽量做到了一起,其中一个上传附件的功能,在基类控制器BaseController 中建了一个Action返回视图,其他需要上传附 ...

  2. Swift中的HTTP请求

    iOS开发中大部分App的网络数据交换是基于HTTP协议的.本文将简单介绍在Swift中使用HTTP进行网络请求的几种方法. 注意:网络请求完成后会获得一个NSData类型的返回数据,如果数据格式为J ...

  3. kettle常见问题解决

    开源ETL工具kettle系列之常见问题 摘要:本文主要介绍使用kettle设计一些ETL任务时一些常见问题,这些问题大部分都不在官方FAQ上,你可以在kettle的论坛上找到一些问题的答案 1. J ...

  4. .Net HttpPost的发送和接收示例代码

    /// <summary> /// 模拟http 发送post或get请求 /// </summary> /// <param name="Url"& ...

  5. Redis 实现高效不遗漏的事件封装

    虽然Redis有订阅功能,但是订阅功能是实时的,过了这个点,就接收不到消息了. 同时,如果订阅的客户端因为某些特殊原因shutdown了,那也就找不回未处理完整的订阅事件了. 但好在,Redis还有一 ...

  6. 关于 Android导出apk时碰到的[Unable to execute dex: Multiple dex files define]

    这是一个编译错误,在ADT的编译器和SDK的工具有差异或是版本不一致时常会出现的一个问题,解决的方案如下: 第一步: updated eclipse (Help->Check for updat ...

  7. Angular学习(8)- directive

    <!DOCTYPE html> <html ng-app="MyApp"> <head> <title>Study 9</ti ...

  8. jquery读取csv文件并用json格式输出

    直接贴上代码: <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Untit ...

  9. of

    “查询序列的一个元素” 1. an element of the query sequence (T) 2. an query sequence element (T) "查询序列或者候选序 ...

  10. php时间日期处理

    php日期函数: 首先想到的就是date(),time(),strtotime(),mktime() strtotime() strtotime()函数用于将英文文本字符串表示的日期转换为时间戳,为 ...