-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesignAddAndSearchWordsDataStructure.java
More file actions
225 lines (186 loc) · 7.41 KB
/
DesignAddAndSearchWordsDataStructure.java
File metadata and controls
225 lines (186 loc) · 7.41 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package Algorithms.Tries;
import java.util.*;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 06 Jan 2026
* @link 211. Design Add and Search Words Data Structure <a href="https://leetcode.com/problems/design-add-and-search-words-data-structure/">LeetCode Link</a>
* @topics HashTable, String, Design, Trie, DFS, BFS
* @companies Datadog(5), Amazon(3), Apple(2), Google(5), Bloomberg(3), Microsoft(2), TikTok(2), Snowflake(2), Meta(4), Rubrik(4), DoorDash(3), LinkedIn(2), Atlassian(2), DocuSign(2), Snap(2)
* @see Algorithms.Tries.ImplementTriePrefixTree
*/
public class DesignAddAndSearchWordsDataStructure {
public static void main(String[] args) {
System.out.println("DesignAddAndSearchWordsDataStructure");
/*
Input:
["WordDictionary","addWord","addWord","addWord","search","search","search","search"]
[ [], ["bad"], ["dad"], ["mad"], ["pad"], ["bad"], [".ad"], ["b.."]]
Output:
[ null, null, null, null, false, true, true, true]
*/
System.out.println("WordDictionary using TrieNode 1");
WordDictionary wd = new WordDictionaryUsingTrieNode1();
wd.addWord("bad");
wd.addWord("dad");
wd.addWord("mad");
System.out.println("inserted words: \nbad, \ndad, \nmad");
System.out.println("search: ");
System.out.println("\"pad\" -> " + wd.search("pad"));
System.out.println("\"bad\" -> " + wd.search("bad"));
System.out.println("\".ad\" -> " + wd.search(".ad"));
System.out.println("\"b..\" -> " + wd.search("b.."));
}
interface WordDictionary {
void addWord(String word);
boolean search(String word);
}
static class WordDictionaryUsingTrieNode1 implements WordDictionary {
static class TrieNode {
boolean isEnd = false;
TrieNode[] neighbors = new TrieNode[26];
}
TrieNode root = new TrieNode();
public void addWord(String word) {
TrieNode curr = root;
for (char c: word.toCharArray()) {
int i = c-'a';
if (curr.neighbors[i]==null) curr.neighbors[i] = new TrieNode();
curr = curr.neighbors[i];
}
curr.isEnd = true;
}
public boolean search(String word) {
// return searchUsingBfs(word);
return searchUsingDfs(word);
}
public boolean searchUsingBfs(String word) { // bfs
Queue<TrieNode> q = new LinkedList<>();
q.add(root);
for (char c: word.toCharArray()) {
int i = c-'a';
int size = q.size();
while (size-- > 0) {
TrieNode curr = q.poll();
if (c == '.') {
for (TrieNode nei: curr.neighbors) {
if (nei != null) q.add(nei);
}
} else {
if (curr.neighbors[i] != null) q.add(curr.neighbors[i]);
}
}
}
while (!q.isEmpty()) {
if (q.poll().isEnd) return true;
}
return false;
}
public boolean searchUsingDfs(String word) { // dfs
// return dfs1(word, root, 0);
return dfs2(word, root, 0);
}
private boolean dfs1(String word, TrieNode node, int i){ // i = word index
for (; i<word.length(); i++) {
char c = word.charAt(i);
if (c == '.') {
for (TrieNode nei: node.neighbors) {
if (nei != null && dfs1(word, nei, i+1)) return true;
}
return false;
} else {
if (node.neighbors[c-'a'] == null) return false;
node = node.neighbors[c-'a'];
}
}
return node.isEnd;
}
private boolean dfs2(String word, TrieNode node, int i){
if (i == word.length()) return node.isEnd;
char c = word.charAt(i);
if (c == '.'){
for(TrieNode nei : node.neighbors) {
if (nei != null && dfs2(word, nei, i+1)) return true;
}
return false;
} else {
if (node.neighbors[c - 'a'] == null) return false;
return dfs2(word, node.neighbors[c - 'a'], i+1);
}
}
}
static class WordDictionaryUsingTrieNode2 implements WordDictionary {
static class TrieNode {
boolean isEnd = false;
Map<Character, TrieNode> neighbors = new HashMap<>();
}
TrieNode root = new TrieNode();
public void addWord(String word) {
TrieNode curr = root;
for (char c: word.toCharArray()) {
if(!curr.neighbors.containsKey(c)) curr.neighbors.put(c, new TrieNode());
curr = curr.neighbors.get(c);
}
curr.isEnd = true;
}
public boolean search(String word) {
// return searchUsingBfs(word);
return searchUsingDfs(word, root);
}
public boolean searchUsingBfs(String word) { // bfs
Queue<TrieNode> q = new LinkedList<>();
q.add(root);
for (char c: word.toCharArray()) {
int size = q.size();
while (size-- > 0) {
TrieNode curr = q.poll();
if (c == '.') {
for (TrieNode nei: curr.neighbors.values()) q.add(nei);
} else {
if (curr.neighbors.get(c) != null) q.add(curr.neighbors.get(c));
}
}
}
while (!q.isEmpty()) {
if (q.poll().isEnd) return true;
}
return false;
}
public boolean searchUsingDfs(String word, TrieNode node) { // dfs
for (int i = 0; i < word.length(); ++i) {
char ch = word.charAt(i);
if (!node.neighbors.containsKey(ch)) {
if (ch == '.') {
for (char x : node.neighbors.keySet()) {
TrieNode nei = node.neighbors.get(x);
if (searchUsingDfs(word.substring(i + 1), nei)) return true;
}
}
return false;
} else {
node = node.neighbors.get(ch);
}
}
return node.isEnd;
}
}
static class WordDictionaryUsingMapBruteForceTLE implements WordDictionary {
Map<Integer, Set<String>> dic = new HashMap<>(); // dictionary
public void addWord(String word) {
int m = word.length();
dic.computeIfAbsent(m, k-> new HashSet<>()).add(word);;
}
public boolean search(String word) {
int len = word.length();
if (dic.containsKey(len)) {
for (String w : dic.get(len)) {
int i = 0;
while (i<len && (word.charAt(i) == '.' || w.charAt(i) == word.charAt(i))) {
i++;
}
if (i == len) return true;
}
}
return false;
}
}
}