Ctrl K

The Word After the Pair

Easy
You are given a text text, made of words separated by single spaces, and two words first and second. Find every place where first is immediately followed by second and then by one more word. Return those third words in the order they appear in the text. Occurrences of the pair may overlap.
Example 1
text="the cat sat on the cat mat and the cat ran", first="the", second="cat" → ['sat', 'mat', 'ran']

The pair "the cat" occurs three times, followed by "sat", "mat" and "ran".

Example 2
text="go go go go", first="go", second="go" → ['go', 'go']

Pairs may overlap: "go go" starts at words 1 and 2, and each is followed by "go". The pair at words 3–4 is last, with nothing after it.

Example 3
text="one two three", first="two", second="three" → []
Constraints
  • 1 ≤ len(text) ≤ 1000
  • text consists of lowercase English letters with single spaces between words and no leading or trailing spaces
  • 1 ≤ len(first), len(second) ≤ 10
  • first and second consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsValue of a Roman Numeral EasyShared Start of Words EasyBracket Balance EasyWhere the Fragment Starts EasyLength of the Final Word Easy
loading Python…
Input
text="the cat sat on the cat mat and the cat ran", first="the", second="cat"
Expected
['sat', 'mat', 'ran']

Run checks the first 3 tests in your browser. Submit checks all 101 tests on the server, hidden ones included.