Occurrences After Bigram

We can solve this problem by splitting the text into words, and then traversing the list of words. For each position, we check if the word at this position and the two preceding words match the pattern “first second third”. If they do, we add the third word to the result.

Python solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution:
    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:
        # Split the text into words.
        words = text.split()

        # Initialize an empty list to store the result.
        result = []

        # Loop through the list of words, starting from the third word.
        for i in range(2, len(words)):
            # If the word at this position and the two preceding words match the pattern "first second third",
            # add the third word to the result.
            if words[i-2] == first and words[i-1] == second:
                result.append(words[i])

        return result

In this code, we start by splitting the input text into words. Then we initialize an empty list to store the result. We traverse the list of words, starting from the third word (since we need two preceding words to match the pattern “first second third”). For each position, we check if the word at this position and the two preceding words match the pattern “first second third”. If they do, we append the third word to the result. After traversing all the words, we return the result.