Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
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.Problem summary: 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".
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"alice is a good girl she is a good student" "a" "good"
"we will we will rock you" "we" "will"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1078: Occurrences After Bigram
class Solution {
public String[] findOcurrences(String text, String first, String second) {
String[] words = text.split(" ");
List<String> ans = new ArrayList<>();
for (int i = 0; i < words.length - 2; ++i) {
if (first.equals(words[i]) && second.equals(words[i + 1])) {
ans.add(words[i + 2]);
}
}
return ans.toArray(new String[0]);
}
}
// Accepted solution for LeetCode #1078: Occurrences After Bigram
func findOcurrences(text string, first string, second string) (ans []string) {
words := strings.Split(text, " ")
n := len(words)
for i := 0; i < n-2; i++ {
if words[i] == first && words[i+1] == second {
ans = append(ans, words[i+2])
}
}
return
}
# Accepted solution for LeetCode #1078: Occurrences After Bigram
class Solution:
def findOcurrences(self, text: str, first: str, second: str) -> List[str]:
words = text.split()
ans = []
for i in range(len(words) - 2):
a, b, c = words[i : i + 3]
if a == first and b == second:
ans.append(c)
return ans
// Accepted solution for LeetCode #1078: Occurrences After Bigram
struct Solution;
impl Solution {
fn find_ocurrences(text: String, first: String, second: String) -> Vec<String> {
let mut res: Vec<String> = vec![];
let words: Vec<&str> = text.split_whitespace().collect();
words.windows(3).for_each(|v| {
if v[0] == first && v[1] == second {
res.push(v[2].to_string());
}
});
res
}
}
#[test]
fn test() {
let text = "alice is a good girl she is a good student".to_string();
let first = "a".to_string();
let second = "good".to_string();
let res: Vec<String> = vec_string!["girl", "student"];
assert_eq!(Solution::find_ocurrences(text, first, second), res);
let text = "we will we will rock you".to_string();
let first = "we".to_string();
let second = "will".to_string();
let res: Vec<String> = vec_string!["we", "rock"];
assert_eq!(Solution::find_ocurrences(text, first, second), res);
}
// Accepted solution for LeetCode #1078: Occurrences After Bigram
function findOcurrences(text: string, first: string, second: string): string[] {
const words = text.split(' ');
const n = words.length;
const ans: string[] = [];
for (let i = 0; i < n - 2; i++) {
if (words[i] === first && words[i + 1] === second) {
ans.push(words[i + 2]);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.