-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path745. Prefix and Suffix Search.java
31 lines (31 loc) · 1.14 KB
/
745. Prefix and Suffix Search.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
class WordFilter {
private HashMap<String, Integer> cache = new HashMap<>();
/**
* For a word like "test", consider "#test", "t#test", "st#test", "est#test",
* "test#test". Then if we have a query like prefix = "te", suffix = "t", we can
* find it by searching for something we've inserted starting with "t#te".
*/
public WordFilter(String[] words) {
String salt = "$@"; int wordIdx = 0;
for (int i = 0; i < words.length; i++) {
for (int j = 0; j < words[i].length(); j++) {
for (int k = 0; k < words[i].length(); k++) {
String key = words[i].substring(0, j+1) + salt + words[i].substring(k);
cache.put(key, i);
}
}
}
}
public int f(String prefix, String suffix) {
String key = prefix+"$@"+suffix;
return cache.getOrDefault(key, -1);
}
}
/**
* Your WordFilter object will be instantiated and called as such:
* WordFilter obj = new WordFilter(words);
* int param_1 = obj.f(prefix,suffix);
*/