Leetcode 1078 - Occurrences After Bigram

Note:

  • Easy simulation problem.

Question:

Given two strings first and second, consider occurrences in some text of the form “first second third“, where second comes immediately after first, and third comes immediately after second.

Return an array of all the words third for each occurrence of “first second third”.

Example:

1
2
Input: text = "alice is a good girl she is a good student", first = "a", second = "good"
Output: ["girl","student"]

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* @param {string} text
* @param {string} first
* @param {string} second
* @return {string[]}
*/
var findOcurrences = function(text, first, second) {
text = text.split(' ');
let ans = [];
for (let i = 0; i < text.length; i++) {
if (text[i] === first && text[i + 1] === second && text[i + 2]) {
ans.push(text[i + 2]);
}
}
return ans;
};