-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrefixAndSuffixSearch_745.java
40 lines (33 loc) · 1.02 KB
/
PrefixAndSuffixSearch_745.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
package org.example;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
public class PrefixAndSuffixSearch_745 {
class WordFilter {
private HashMap<String,Integer> map = new HashMap<>();
public WordFilter(String[] words) {
for (int i=0;i<words.length;i++){
String word = words[i];
for (int k=0;k<word.length();k++){
String sub1 = word.substring(0,k+1);
for (int j=word.length()-1;j>=0;j--){
String sub2 = word.substring(j);
String combine = sub1+"|"+sub2;
map.put(combine, i);
}
}
}
}
public int f(String pref, String suff) {
return map.getOrDefault(pref+"|"+suff, -1);
}
}
class Node {
public int idxs;
public Node[][] children;
Node(){
idxs = -1;
children = new Node[26][26];
}
}
}