Problem statement

https://leetcode.com/problems/occurrences-after-bigram/

Solution

Split into words, then find for our pattern.

Complexity

It is O(n * k) for time, where k = max(len(s1), len(s2)) and O(n) for space.

Code

class Solution:
    def findOcurrences(self, text, s1, s2):
        words, ans = text.split(), []
        for i in range(len(words) - 2):
            if words[i] == s1 and words[i+1] == s2:
                ans += [words[i+2]]
        return ans