09 Sept 2026Go / Python / TypeScriptEasy

Occurrences After Bigram

Return every word that immediately follows a specified two-word sequence in text.

Split the text and inspect each consecutive triple, emitting the third word when the first two match the bigram.

complexity

O(n) time and O(n) token space.

solution files

  • Go occurrences-after-bigram/solution.go
  • Python occurrences-after-bigram/solution.py
  • TypeScript occurrences-after-bigram/solution.ts

Solution files

Gooccurrences-after-bigram/solution.go
package main

import "strings"

func findOcurrences(text string, first string, second string) []string {
	words := strings.Fields(text)
	result := []string{}
	for index := 2; index < len(words); index++ {
		if words[index-2] == first && words[index-1] == second {
			result = append(result, words[index])
		}
	}
	return result
}
Pythonoccurrences-after-bigram/solution.py
class Solution:
    def findOcurrences(self, text: str, first: str, second: str) -> list[str]:
        words = text.split()
        return [words[index] for index in range(class="syntax-number">2, len(words)) if words[index - class="syntax-number">2] == first and words[index - class="syntax-number">1] == second]
TypeScriptoccurrences-after-bigram/solution.ts
function findOcurrences(text: string, first: string, second: string): string[] {
  const words = text.split(class="syntax-string">" "); const result: string[] = [];
  for (let index = class="syntax-number">2; index < words.length; index += class="syntax-number">1) if (words[index - class="syntax-number">2] === first && words[index - class="syntax-number">1] === second) result.push(words[index]);
  return result;
}