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.

题意:

设计一个数据结构,能够插入单词,能够查找字符串,并支持正则表达式中的“.”单字符通配。

思路:

这是一道很典型的字典树(Trie)的题目,唯一的变化是在字典树查找的过程中引入了通配符.,通配符匹配可以通过回溯法(枚举26个英文)实现。

字典树(Trie)是面试中常见的题型,也叫做前缀树,是一种用来快速检索的多叉树结构。对于本题而言,需要构造的是一个英文字母字典树,因此是一个26叉树。

字典树可以利用字符串的公共前缀来节约存储的空间并加快检索的速度。下图中的字典树包含了五个英文单词dad, do, dog, done, bi:

字典树有如下基本性质:

  1. 字典树的根节点不包含字符,代表一个空字符串;
  2. 从根节点到某节点,路径上经过的字符连接起来即为该节点所代表的字符串;
  3. 每个节点所代表的字符串各不相同,父节点代表的字符串一定是它孩子节点代表的字符串的前缀

代码:

 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) { // bad
TrieNode node = root; // 初始化node
for(int i = 0; i< word.length();i++){ // 遍历bad的每个char
int j = word.charAt(i)-'a'; // j = 62-61 = 1
if(node.children[j]==null){ //
node.children[j]=new TrieNode();
}
node = node.children[j];
}
node.isWord = true;
node.word = 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 find(word,root,0);
} // recursively checking each char
public boolean find(String word, TrieNode node, int index){
if(index == word.length()) return node.isWord;
if(word.charAt(index) =='.') {
for(TrieNode temp : node.children){
if(temp!=null && find(word,temp,index+1)) return true;
}
return false;
} else{
int j = word.charAt(index) - 'a';
TrieNode temp = node.children[j];
return temp !=null && find(word, temp, index+1);
}
}
} class TrieNode{
TrieNode[] children;
boolean isWord;
String word; public TrieNode(){
children = new TrieNode[26];
isWord = false;
word = "";
}
}

[leetcode]211. Add and Search Word - Data structure design添加查找单词 - 数据结构设计的更多相关文章

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

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

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

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

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

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

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

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

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

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

    设计一个支持以下两个操作的数据结构:void addWord(word)bool search(word)search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z . ...

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

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

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

随机推荐

  1. Centos 6.5将光盘作为yum源的设置方法

    Centos 6.5将光盘作为yum源的设置方法 在使用Centos 的时候,用yum来安装软件包是再方便不过了,但是如果在无法连接互联网的情况下,yum就不好用了. 下面介绍一种方式,就是将Cent ...

  2. python并发编程之多进程理论部分

    原文连接:http://www.cnblogs.com/linhaifeng/articles/7430066.html#_label4 一 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责 ...

  3. MongoDB集群与LBS应用系列(二)--与Hadoop集成

    长期以来,我每开个系列,只有兴趣写一篇,很难持之与恒.为了克服这个长久以来的性格弱点,以及梳理工作半年的积累.最近一个月会写两篇关于Mongo在地理大数据方面的实践和应用,一篇关于推荐系统的初期准备过 ...

  4. tensorflow读取数据

    线程和队列 在使用TensorFlow进行异步计算时,队列是一种强大的机制. 为了感受一下队列,让我们来看一个简单的例子.我们先创建一个“先入先出”的队列(FIFOQueue),并将其内部所有元素初始 ...

  5. C语言键盘按键无阻塞侦测:kbhit()

    http://www.360doc.com/content/12/0414/09/1317564_203474440.shtml kbhit in c kbhit in c: kbhit functi ...

  6. 《GPU高性能编程CUDA实战》附录三 关于book.h

    ▶ 本书中用到的公用函数放到了头文件book.h中 #ifndef __BOOK_H__ #define __BOOK_H__ #include <stdio.h> #include &l ...

  7. mysql 5.7新特新 操作json 数组

    ; UPDATE EDI.edi_history SET response_summary = JSON_REPLACE(response_summary, ; 对于json数组,使用$[*]  然后 ...

  8. myeclipse 代码提示

    from http://fuyiyuan2011.iteye.com/blog/1258264 在软件开发过程中,有了代码提示能使开发能够更加快捷与便利.但在Eclipse ,MyEclipse等ja ...

  9. windows平台下 c++获取 系统版本 网卡 内存 CPU 硬盘 显卡信息<转>

    GetsysInfo.h: #ifndef _H_GETSYSINFO #define _H_GETSYSINFO #pragma once #include <afxtempl.h> c ...

  10. python os模块常用命令

    python编程时,经常和文件.目录打交道,这是就离不了os模块.os模块包含普遍的操作系统功能,与具体的平台无关.以下列举常用的命令 1. os.name()——判断现在正在实用的平台,Window ...