- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1065.java
51 lines (48 loc) · 1.8 KB
/
_1065.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
packagecom.fishercoder.solutions.secondthousand;
importjava.util.ArrayList;
importjava.util.Arrays;
importjava.util.Collections;
importjava.util.List;
publicclass_1065 {
publicstaticclassSolution1 {
publicint[][] indexPairs(Stringtext, String[] words) {
List<List<Integer>> lists = newArrayList<>();
for (Stringword : words) {
lists.addAll(findAllMatchsForThisWord(word, text));
}
if (lists.isEmpty()) {
returnnewint[][] {};
}
Collections.sort(
lists,
(o1, o2) -> {
if (o1.get(0) > o2.get(0)) {
return1;
} elseif (o1.get(0) < o2.get(0)) {
return -1;
} else {
if (o1.get(1) > o2.get(1)) {
return1;
} else {
return -1;
}
}
});
int[][] result = newint[lists.size()][lists.get(0).size()];
for (inti = 0; i < lists.size(); i++) {
result[i][0] = lists.get(i).get(0);
result[i][1] = lists.get(i).get(1);
}
returnresult;
}
privateList<List<Integer>> findAllMatchsForThisWord(Stringword, Stringtext) {
List<List<Integer>> lists = newArrayList<>();
for (inti = 0; i <= text.length() - word.length(); i++) {
if (text.substring(i, i + word.length()).equals(word)) {
lists.add(Arrays.asList(i, i + word.length() - 1));
}
}
returnlists;
}
}
}