-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOccurrencesAfterBigram.java
More file actions
64 lines (58 loc) · 1.74 KB
/
OccurrencesAfterBigram.java
File metadata and controls
64 lines (58 loc) · 1.74 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
package leetcode;
import java.util.LinkedList;
import java.util.List;
/**
* OccurrencesAfterBigram
* https://leetcode-cn.com/problems/occurrences-after-bigram/
* 1078. Bigram 分词
* https://leetcode-cn.com/problems/occurrences-after-bigram/solution/mo-ni-pi-pei-by-oshdyr-3298/
*
* @author tobin
* @since 2021-12-26
*/
public class OccurrencesAfterBigram {
public static void main(String[] args) {
OccurrencesAfterBigram sol = new OccurrencesAfterBigram();
String[] res1 = sol.findOcurrences(
"alice is a good girl she is a good student",
"a",
"good");
for (String str : res1) {
System.out.println(str);
}
System.out.println("===");
}
public String[] findOcurrences(String text, String first, String second) {
if (text == null || text.length() < 1) {
return null;
}
String[] parts = text.split(" ");
if (parts.length < 1) {
return null;
}
List<String> resList = new LinkedList<>();
boolean lastIsSecond = false;
boolean lastIsFirst = false;
for (String curr : parts) {
if (lastIsSecond) {
resList.add(curr);
lastIsSecond = false;
}
if (lastIsFirst) {
if (curr.equals(second)) {
lastIsSecond = true;
}
lastIsFirst = false;
}
if (curr.equals(first)) {
lastIsFirst = true;
}
}
String[] resArr = new String[resList.size()];
int idx = 0;
for (String res : resList) {
resArr[idx++] = res;
}
return resArr;
}
}