forked from paopao2/leetcode-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdd and Search Word - Data structure design.js
132 lines (108 loc) · 2.81 KB
/
Add and Search Word - Data structure design.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
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.
*/
// MEMORY LIMIT EXCEEDED...
/**
* @constructor
*/
var WordDictionary = function() {
this.root = TrieNode();
};
var TrieNode = function() {
var isEnd,
links = {};
return {
containsKey: function(n) {
return links[n] !== undefined;
},
get: function(ch) {
return links[ch];
},
put: function(ch, node) {
links[ch] = node;
},
setEnd: function() {
isEnd = true;
},
isEnd: function() {
return isEnd;
},
getLinks: function() {
return links;
}
};
};
/**
* @param {string} word
* @return {void}
* Adds a word into the data structure.
*/
WordDictionary.prototype.addWord = function(word) {
var len = word.length,
node = this.root,
ch,
i;
for (i = 0; i < len; i++) {
ch = word.charAt(i);
if (!node.containsKey(ch)) {
node.put(ch, TrieNode());
}
node = node.get(ch);
}
node.setEnd();
};
/**
* @param {string} word
* @return {boolean}
* Returns if the word is in the data structure. A word could
* contain the dot character '.' to represent any one letter.
*/
WordDictionary.prototype.search = function(word) {
var node = this.root;
return this.searchHelper(word, node, 0);
};
WordDictionary.prototype.searchHelper = function(word, node, index) {
var links,
ch,
i,
j;
if (index === word.length) {
if (node.isEnd()) {
return true;
}
return false;
}
ch = word.charAt(index);
if (ch === '.') {
links = node.getLinks();
for (j in links) {
if (this.searchHelper(word, links[j], index + 1)) {
return true;
}
}
} else if (node.containsKey(ch)) {
return this.searchHelper(word, node.get(ch), index + 1);
}
return false;
}
/**
* Your WordDictionary object will be instantiated and called as such:
* var wordDictionary = new WordDictionary();
* wordDictionary.addWord("word");
* wordDictionary.search("pattern");
*/