Skip to content

Commit d8799c8

Browse files
committed
feat: add solutions to lc problem: No.1078
No.1078.Occurrences After Bigram
1 parent 6f86bde commit d8799c8

File tree

6 files changed

+146
-2
lines changed

6 files changed

+146
-2
lines changed

solution/1000-1099/1078.Occurrences After Bigram/README.md

+55-1
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,76 @@
3939

4040
<!-- 这里可写通用的实现逻辑 -->
4141

42+
将 text 按空格切分为 words 列表,然后遍历 words,判断是否满足 `words[i] == first && words[i + 1] == second`,若是,则将 `words[i + 2]` 添加至结果列表 ans 中。
43+
44+
最后返回 ans 即可。
45+
4246
<!-- tabs:start -->
4347

4448
### **Python3**
4549

4650
<!-- 这里可写当前语言的特殊实现逻辑 -->
4751

4852
```python
49-
53+
class Solution:
54+
def findOcurrences(self, text: str, first: str, second: str) -> List[str]:
55+
words = text.split(' ')
56+
return [words[i + 2] for i in range(len(words) - 2) if words[i] == first and words[i + 1] == second]
5057
```
5158

5259
### **Java**
5360

5461
<!-- 这里可写当前语言的特殊实现逻辑 -->
5562

5663
```java
64+
class Solution {
65+
66+
public String[] findOcurrences(String text, String first, String second) {
67+
String[] words = text.split(" ");
68+
List<String> ans = new ArrayList<>();
69+
for (int i = 0; i < words.length - 2; ++i) {
70+
if (first.equals(words[i]) && second.equals(words[i + 1])) {
71+
ans.add(words[i + 2]);
72+
}
73+
}
74+
return ans.toArray(new String[0]);
75+
}
76+
}
77+
78+
```
79+
80+
### **C++**
81+
82+
```cpp
83+
class Solution {
84+
public:
85+
vector<string> findOcurrences(string text, string first, string second) {
86+
istringstream is(text);
87+
vector<string> words;
88+
string word;
89+
while (is >> word) words.push_back(word);
90+
vector<string> ans;
91+
for (int i = 0; i < words.size() - 2; ++i)
92+
if (words[i] == first && words[i + 1] == second)
93+
ans.push_back(words[i + 2]);
94+
return ans;
95+
}
96+
};
97+
```
5798
99+
### **Go**
100+
101+
```go
102+
func findOcurrences(text string, first string, second string) []string {
103+
words := strings.Split(text, " ")
104+
var ans []string
105+
for i := 0; i < len(words)-2; i++ {
106+
if words[i] == first && words[i+1] == second {
107+
ans = append(ans, words[i+2])
108+
}
109+
}
110+
return ans
111+
}
58112
```
59113

60114
### **...**

solution/1000-1099/1078.Occurrences After Bigram/README_EN.md

+51-1
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,63 @@
5252
### **Python3**
5353

5454
```python
55-
55+
class Solution:
56+
def findOcurrences(self, text: str, first: str, second: str) -> List[str]:
57+
words = text.split(' ')
58+
return [words[i + 2] for i in range(len(words) - 2) if words[i] == first and words[i + 1] == second]
5659
```
5760

5861
### **Java**
5962

6063
```java
64+
class Solution {
65+
66+
public String[] findOcurrences(String text, String first, String second) {
67+
String[] words = text.split(" ");
68+
List<String> ans = new ArrayList<>();
69+
for (int i = 0; i < words.length - 2; ++i) {
70+
if (first.equals(words[i]) && second.equals(words[i + 1])) {
71+
ans.add(words[i + 2]);
72+
}
73+
}
74+
return ans.toArray(new String[0]);
75+
}
76+
}
77+
78+
```
79+
80+
### **C++**
81+
82+
```cpp
83+
class Solution {
84+
public:
85+
vector<string> findOcurrences(string text, string first, string second) {
86+
istringstream is(text);
87+
vector<string> words;
88+
string word;
89+
while (is >> word) words.push_back(word);
90+
vector<string> ans;
91+
for (int i = 0; i < words.size() - 2; ++i)
92+
if (words[i] == first && words[i + 1] == second)
93+
ans.push_back(words[i + 2]);
94+
return ans;
95+
}
96+
};
97+
```
6198
99+
### **Go**
100+
101+
```go
102+
func findOcurrences(text string, first string, second string) []string {
103+
words := strings.Split(text, " ")
104+
var ans []string
105+
for i := 0; i < len(words)-2; i++ {
106+
if words[i] == first && words[i+1] == second {
107+
ans = append(ans, words[i+2])
108+
}
109+
}
110+
return ans
111+
}
62112
```
63113

64114
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
vector<string> findOcurrences(string text, string first, string second) {
4+
istringstream is(text);
5+
vector<string> words;
6+
string word;
7+
while (is >> word) words.push_back(word);
8+
vector<string> ans;
9+
for (int i = 0; i < words.size() - 2; ++i)
10+
if (words[i] == first && words[i + 1] == second)
11+
ans.push_back(words[i + 2]);
12+
return ans;
13+
}
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func findOcurrences(text string, first string, second string) []string {
2+
words := strings.Split(text, " ")
3+
var ans []string
4+
for i := 0; i < len(words)-2; i++ {
5+
if words[i] == first && words[i+1] == second {
6+
ans = append(ans, words[i+2])
7+
}
8+
}
9+
return ans
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public String[] findOcurrences(String text, String first, String second) {
3+
String[] words = text.split(" ");
4+
List<String> ans = new ArrayList<>();
5+
for (int i = 0; i < words.length - 2; ++i) {
6+
if (first.equals(words[i]) && second.equals(words[i + 1])) {
7+
ans.add(words[i + 2]);
8+
}
9+
}
10+
return ans.toArray(new String[0]);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def findOcurrences(self, text: str, first: str, second: str) -> List[str]:
3+
words = text.split(' ')
4+
return [words[i + 2] for i in range(len(words) - 2) if words[i] == first and words[i + 1] == second]

0 commit comments

Comments
 (0)