Leetcode 1156 Solution

This article provides solution to leetcode question 1156 (occurrences-after-bigram)

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

Solution

class Solution:
    def findOcurrences(self, text: str, first: str, second: str) -> List[str]:
        ans = []
        targets = [first, second]
        i, j = 0, 0

        words = text.split()
        while i < len(words):
            while i < len(words) and j < len(targets) and targets[j] == words[i]:
                j += 1
                i += 1

            if i < len(words) and j == len(targets):
                ans.append(words[i])

            if j == 0:
                i += 1
            j = 0

        return ans