Given the first word first and the second word second, consider every occurrence in a text text of the form "first second third", where second appears immediately after first, and third appears immediately after second.
For every such occurrence, add the third word "third" to the answer and return the answer.
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 <= 1000textconsists of lowercase English letters and spaces.- All words in
textare separated by a single space character. 1 <= first.length, second.length <= 10firstandsecondconsist of lowercase English letters.
Analysis#
While traversing the array, simply check whether the current words satisfy the conditions given in the problem.
Rust Code#
impl Solution {
pub fn find_ocurrences(text: String, first: String, second: String) -> Vec<String> {
let mut is_one = false;
let mut is_two = false;
let mut res = vec![];
let mut pre1 = "";
let mut pre2 = "";
for word in text.split(" ") {
if pre1 == first && pre2 == second {
res.push(word.to_string());
}
pre1 = pre2;
pre2 = word;
}
res
}
}
