forked from szl0072/Leetcode-Solution-Code
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAddandSearchWord.java
85 lines (73 loc) · 2.32 KB
/
AddandSearchWord.java
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
package leetcode;
/**
* Project Name : Leetcode
* Package Name : leetcode
* File Name : AddandSearchWord
* Creator : Edward
* Date : Sep, 2017
* Description : TODO
*/
class TrieNode {
TrieNode[] children;
boolean isWord;
String word;
public TrieNode() {
children = new TrieNode[26];
isWord = false;
word = "";
}
}
public class AddandSearchWord {
/**
* 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.
For example:
addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -> false
search("bad") -> true
search(".ad") -> true
search("b..") -> true
*/
private TrieNode root;
/** Initialize your data structure here. */
public AddandSearchWord() {
root = new TrieNode();
}
/** Adds a word into the data structure. */
// time : O(n) n: word.length();
// O(num of TrieNode * 26) = O(num of Words * word.length() * 26)
public void addWord(String word) {
TrieNode node = root;
for (int i = 0; i < word.length(); i++) {
int j = word.charAt(i) - 'a';
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);
}
public boolean find(String word, TrieNode node, int index) {
if (index == word.length()) return node.isWord;//!node.word.equals("");
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);
}
}
}