-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiStringSearch.java
55 lines (50 loc) · 1.35 KB
/
MultiStringSearch.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
import java.util.*;
class Program {
// O(ns + bs) time | O(ns) space
public static List<Boolean> multiStringSearch(String bigString, String[] smallStrings) {
// Write your code here.
List<Boolean> res = new ArrayList<Boolean>();
Trie trie = new Trie();
for (String smallStr : smallStrings) {
trie.add(smallStr);
}
Set<String> containedStrings = new HashSet<>();
for (int i = 0; i < bigString.length(); i++) {
findSmallStringsIn(bigString.substring(i), trie, containedStrings);
}
for (String smallStr : smallStrings) {
res.add(containedStrings.contains(smallStr));
}
return res;
}
private static void findSmallStringsIn(String str, Trie trie, Set<String> containedStrings) {
Trie node = trie;
for (char letter : str.toCharArray()) {
if (!node.children.containsKey(letter)) {
break;
}
node = node.children.get(letter);
if (!node.word.equals("")) {
containedStrings.add(node.word);
}
}
}
static class Trie {
Map<Character, Trie> children;
String word;
public Trie() {
this.children = new HashMap<>();
this.word = "";
}
public void add(String str) {
Trie node = this;
for (char letter : str.toCharArray()) {
if (!node.children.containsKey(letter)) {
node.children.put(letter, new Trie());
}
node = node.children.get(letter);
}
node.word = str;
}
}
}