[LeetCode][JavaScript]Add and Search Word - Data structure design

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.

https://leetcode.com/problems/add-and-search-word-data-structure-design/



字典树,在上一题的基础上稍做修改。http://www.cnblogs.com/Liok3187/p/4626730.html

遇到‘.‘就要采用递归的方式,遍历这一层词典里所有的词。

 1 /**
 2  * @constructor
 3  */
 4 var WordDictionary = function() {
 5     this.root = new TrieNode(‘root‘);
 6 };
 7
 8 function TrieNode(key) {
 9     return {
10         key : key,
11         isWord : false
12     };
13 }
14
15 /**
16  * @param {string} word
17  * @return {void}
18  * Adds a word into the data structure.
19  */
20 WordDictionary.prototype.addWord = function(word) {
21     var tree = this.root, i, curr;
22     for(i = 0; i < word.length; i++){
23         curr = word[i];
24         if(!tree[curr]){
25             tree[curr] = new TrieNode(curr);
26         }
27         tree = tree[curr];
28     }
29     tree.isWord = true;
30 };
31
32 /**
33  * @param {string} word
34  * @return {boolean}
35  * Returns if the word is in the data structure. A word could
36  * contain the dot character ‘.‘ to represent any one letter.
37  */
38 WordDictionary.prototype.search = function(word) {
39     return searchWord(word, this.root);
40
41     function searchWord(word, tree){
42         if(word === "" && tree.isWord){
43             return true;
44         }
45         if(word[0] !== ‘.‘){
46             if(!tree[word[0]]){
47                 return false;
48             }else{
49                 return searchWord(word.substring(1, word.length), tree[word[0]]);
50             }
51         }else{
52             for(var i in tree){
53                 if(i === ‘key‘ || i === ‘isWord‘){
54                     continue;
55                 }
56                 if(searchWord(word.substring(1, word.length), tree[i])){
57                     return true;
58                 }
59             }
60             return false;
61         }
62     }
63 };
时间: 2024-10-24 07:55:11

[LeetCode][JavaScript]Add and Search Word - Data structure design的相关文章

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

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

[LeetCode] 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

LeetCode 211. Add and Search Word - Data structure design(字典树)

题目 字典树. class WordDictionary { public: int map[100005][26]; int tag[100005]; int num; /** Initialize your data structure here. */ WordDictionary() { memset(map,0,sizeof(map)); memset(tag,0,sizeof(tag)); num=0; } /** Adds a word into the data structur

[LeetCode] 211. Add and Search Word - Data structure design Java

题目: 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 le

LeetCode 211. Add and Search Word - Data structure design

原题 设计一个包含下面两个操作的数据结构:addWord(word), search(word)addWord(word)会在数据结构中添加一个单词.而search(word)则支持普通的单词查询或是只包含. 和a-z的简易正则表达式的查询.一个 . 可以代表一个任何的字母. 样例 addWord("bad") addWord("dad") addWord("mad") search("pad") // return fals

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

LeetCode——Add and Search Word - Data structure design

Description: 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 a

[LeetCode] 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