-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path211_AddandSearchWord-DataStructureDesign.java
More file actions
70 lines (63 loc) · 2.13 KB
/
211_AddandSearchWord-DataStructureDesign.java
File metadata and controls
70 lines (63 loc) · 2.13 KB
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
// 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.
class TrieNode {
boolean isEnd;
TrieNode[] children;
public TrieNode() {
this.isEnd = false;
this.children = new TrieNode[26];
}
}
public class WordDictionary {
private TrieNode root;
public WordDictionary() {
root = new TrieNode();
}
// Adds a word into the data structure.
public void addWord(String word) {
TrieNode runner = root;
for(char c : word.toCharArray()) {
if(runner.children[c - 'a'] == null) {
runner.children[c - 'a'] = new TrieNode();
}
runner = runner.children[c - 'a'];
}
runner.isEnd = true;
}
// 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) {
TrieNode runner = root;
return searchWord(word, runner, 0);
}
public boolean searchWord(String word, TrieNode runner, int i) {
if(i == word.length()) return runner.isEnd;
char c = word.charAt(i);
if(c == '.') {
for(int j=0; j<26; j++) {
if(runner.children[j] != null && searchWord(word, runner.children[j], i + 1)) {
return true;
}
}
return false;
} else {
return runner.children[c - 'a'] != null && searchWord(word, runner.children[c - 'a'], i + 1);
}
}
}
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");