-
Notifications
You must be signed in to change notification settings - Fork 310
/
Copy pathadd-and-search-word-data-structure-design.js
82 lines (69 loc) · 1.74 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
// Source : https://leetcode.com/problems/add-and-search-word-data-structure-design/
// Author : Han Zichi
// Date : 2016-08-17
// Runtime: 240 ms
// Your runtime beats 100.00% of javascript submissions
function Node() {
this.nodes = [];
this.endFlag = false;
}
/**
* @constructor
*/
var WordDictionary = function() {
this.startNode = new Node();
// 以该 node 结尾的单词存在
this.startNode.endFlag = true;
};
/**
* @param {string} word
* @return {void}
* Adds a word into the data structure.
*/
WordDictionary.prototype.addWord = function(word) {
var node = this.startNode;
for (var i = 0, len = word.length; i < len; i++) {
var item = word.charCodeAt(i) - 97;
if (!node.nodes[item]) {
node.nodes[item] = new Node();
}
node = node.nodes[item];
}
node.endFlag = true;
};
/**
* @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.startNode;
var isFound = false;
dfs(node, 0);
function dfs(node, index) {
if (isFound)
return;
if (index === word.length) {
isFound = node.endFlag;
return;
}
if (word[index] === '.') {
for (var i = 0; i < 26; i++) {
if (node.nodes[i])
dfs(node.nodes[i], index + 1);
}
} else {
var item = word.charCodeAt(index) - 97;
if (node.nodes[item])
dfs(node.nodes[item], index + 1);
}
}
return isFound;
};
/**
* Your WordDictionary object will be instantiated and called as such:
* var wordDictionary = new WordDictionary();
* wordDictionary.addWord("word");
* wordDictionary.search("pattern");
*/