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".
Example 1:
Input: text = "alice is a good girl she is a good student", first = "a", second = "good" Output: ["girl","student"]
Example 2:
Input: text = "we will we will rock you", first = "we", second = "will" Output: ["we","rock"]
Constraints:
1 <= text.length <= 1000text consists of lowercase English letters and spaces.text are separated by a single space.1 <= first.length, second.length <= 10first and second consist of lowercase English letters.text will not have any leading or trailing spaces.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We're given some text and two words (a bigram). The goal is to find all words that appear directly after the bigram in the text. The brute force method involves examining every possible position in the text and checking if the bigram exists at that position.
Here's how the algorithm would work step-by-step:
def find_occurrences_after_bigram(text, first_word, second_word):
words = text.split()
result = []
# Iterate through the words, stopping early enough to check for the bigram
for index in range(len(words) - 2):
# Check if the current word and the next word match the bigram
if words[index] == first_word and words[index + 1] == second_word:
# Add the word after the bigram to the result
result.append(words[index + 2])
return resultThe problem asks us to find words that appear immediately after a specific pair of words. The trick is to efficiently scan the text only once, keeping track of the previous two words. If they match our target pair, we record the following word.
Here's how the algorithm would work step-by-step:
def find_occurrences(text, first_word, second_word):
words = text.split()
result = []
# Iterate through the words starting from the third word.
for i in range(2, len(words)):
# Check if the previous two words match the target bigram.
if words[i - 2] == first_word and words[i - 1] == second_word:
# Add the current word to the result.
result.append(words[i])
return result| Case | How to Handle |
|---|---|
| Empty text string | Return an empty list immediately, as there can be no bigram occurrences. |
| Text string with less than three words | Return an empty list immediately, as a bigram requires at least two words followed by a potential third. |
| bigram first and second words are identical | The solution should correctly identify occurrences even if the bigram words are the same. |
| Text contains only the bigram, repeated multiple times | The solution should identify all occurrences where the bigram is followed by another word. |
| Very long text string (scalability) | Ensure the solution's time complexity is linear or near-linear (e.g., O(n)) for efficient processing. |
| bigram words are at the very end of the text string | The code needs to avoid index out-of-bounds errors when checking for the third word following the bigram. |
| The third word (after the bigram) appears multiple times in the text. | The solution should correctly identify all instances of the third word following the specified bigram, and add them to the output. |
| Case sensitivity in word matching (e.g., 'the' vs 'The'). | Convert all words to lowercase before comparison to ensure case-insensitive matching. |