-
Notifications
You must be signed in to change notification settings - Fork 23
/
211. Add and Search Word - Data structure design.java
executable file
·96 lines (79 loc) · 2.92 KB
/
211. Add and Search Word - Data structure design.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
86
87
88
89
90
91
92
93
94
95
96
M
tags: Trie, Design, Backtracking
time: O(n) to search and to add word
space: < O(mn), depends on the input. m = # of words
#### Trie, prefix tree.
- Trie Structure: `boolean isEnd`, `HashMap<Character, TrieNode> children`
- trie.addWord: 没node就加,有node就移动
- trie.search: 没node就return false,有node就移动
- Alternatively, the hash can be `TrieNode[26]` a fixed size array when applicable
- I like map better for the simplicity to write (w/o converting char -> index)
```
/*
Design a data structure that supports the following two operations: addWord(word) and 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.
Example
addWord("bad")
addWord("dad")
addWord("mad")
search("pad") // return false
search("bad") // return true
search(".ad") // return true
search("b..") // return true
Note
You may assume that all words are consist of lowercase letters a-z.
Tags Expand
Trie
*/
/*
Build the WordDictionary like a TrieTree.
Note: the '.' indicates any letter, which means we'd have to traverse through all possible letters in current level.
Only one of the returning search results needs to be true
Note:
TrieNode contains that char, boolean, and HashMap of children
*/
public class WordDictionary {
class TrieNode{
HashMap<Character, TrieNode> children = new HashMap<Character, TrieNode>();
boolean isEnd = false;
}
TrieNode root;
public WordDictionary(){
this.root = new TrieNode();
}
// Adds a word into the data structure.
public void addWord(String word) {
TrieNode node = root;
for (char c : word.toCharArray()) {
node.children.putIfAbsent(c, new TrieNode());
node = node.children.get(c);
}
node.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) {
return searchHelper(root, word, 0);
}
public boolean searchHelper(TrieNode node, String word, int index) {
if (index == word.length()) return node.isEnd;
HashMap<Character, TrieNode> children = node.children;
char c = word.charAt(index);
if (children.containsKey(c)) {
return searchHelper(children.get(c), word, index + 1);
} else if (c == '.'){ // wild card
for (TrieNode childNode : children.values()) {
if (searchHelper(childNode, word, index + 1)) return true;
}
}
return false; // all search failed
}
}
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/
```