Occurrences After Bigram - Problem
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".
Input & Output
Example 1 — Basic Pattern Matching
$
Input:
text = "alice is a girl she is a student", first = "a", second = "girl"
›
Output:
["she"]
💡 Note:
The pattern "a girl" appears once in the text, followed by "she", so we return ["she"]
Example 2 — Multiple Occurrences
$
Input:
text = "we will we will rock you", first = "we", second = "will"
›
Output:
["we","rock"]
💡 Note:
Pattern "we will" appears twice: first followed by "we", second followed by "rock"
Example 3 — No Matches
$
Input:
text = "alice is a girl", first = "bob", second = "is"
›
Output:
[]
💡 Note:
The pattern "bob is" never appears in the text, so return empty array
Constraints
- 1 ≤ text.length ≤ 1000
- text consists of lowercase English letters and spaces
- All words in text are separated by a single space
- 1 ≤ first.length, second.length ≤ 10
- first and second consist of lowercase English letters
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code