-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiSearch.java
47 lines (41 loc) · 1.14 KB
/
multiSearch.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
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class multiSearch {
private int[][] res;
private String global_big;
public int[][] multiSearch(String big, String[] smalls) {
int len = smalls.length;
res = new int[len][];
global_big = big;
for (int i = 0; i < len; ++i)
addToRes(i, smalls[i]);
return res;
}
private void addToRes(int position, String str) {
if (str.equals("")) {
res[position] = new int[0];
return;
}
LinkedList<Integer> list = new LinkedList<>();
int idx = 0;
int len = global_big.length();
while (idx < len && idx + str.length() < len) {
String tem = global_big.substring(idx, idx + str.length());
if (str.equals(tem)) {
list.addLast(idx);
}
idx++;
}
// while ((idx = global_big.indexOf(str, idx) + 1) != 0)
// list.addLast(idx - 1);
res[position] = list.stream().mapToInt(Integer::intValue).toArray();
}
public static void main(String[] args) {
multiSearch obj = new multiSearch();
String s = "mississippi";
String[] str = {"is","ppi","hi","sis","i","ssippi"};
int[][] res = obj.multiSearch(s, str);
System.out.print(res);
}
}