-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path1078.Occurrences-After-Bigram.java
49 lines (40 loc) · 1.25 KB
/
1078.Occurrences-After-Bigram.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
// https://leetcode.com/problems/occurrences-after-bigram/
//
// algorithms
// Easy (69.8%)
// Total Accepted: 2,625
// Total Submissions: 3,761
// beats 100.0% of java submissions
class Solution {
public String[] findOcurrences(String text, String first, String second) {
String[] wordArr = text.split(" ");
int len = wordArr.length;
if (len < 3) {
return new String[]{};
}
int i = 0;
ArrayList<String> l = new ArrayList<>();
while (i < len - 2) {
if (wordArr[i].equals(first) && wordArr[i + 1].equals(second)) {
l.add(wordArr[i + 2]);
}
i++;
}
String[] res = l.toArray(new String[l.size()]);
return res;
}
}
class Solution {
public String[] findOcurrences(String text, String first, String second) {
ArrayList<String> l = new ArrayList<>();
String[] arr = text.split(" ");
int idx = 0, len = arr.length;
while (idx < len) {
if (idx < len - 2 && arr[idx].equals(first) && arr[idx + 1].equals(second)) {
l.add(arr[idx + 2]);
}
idx++;
}
return l.toArray(new String[0]);
}
}