java:
每日一题简单题 看看题解 笑
class Solution {
public String[] findOcurrences(String text, String first, String second) {
String[] stext = text.split(" ");
int n = stext.length;
List<String> list = new ArrayList<>();
for(int i = 0; i + 2 < n; i++){
if(stext[i].equals(first) && stext[i + 1].equals(second)){
list.add(stext[i + 2]);
}
}
return list.toArray(new String[list.size()]);
}
}
python3:
class Solution:
def findOcurrences(self, text: str, first: str, second: str) -> List[str]:
ss = text.split()
ans = []
for i in range(len(text)):
if i + 1 < len(ss) and ss[i] == first and ss[i+1] == second:
if i + 2 < len(ss):
ans.append(ss[i+2])
i += 1
return ans