1078. Occurrences After Bigram LeetCode Solution

In this guide, you will get 1078. Occurrences After Bigram LeetCode Solution with the best time and space complexity. The solution to Occurrences After Bigram problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Occurrences After Bigram solution in C++
  4. Occurrences After Bigram solution in Java
  5. Occurrences After Bigram solution in Python
  6. Additional Resources
1078. Occurrences After Bigram LeetCode Solution image

Problem Statement of Occurrences After Bigram

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 <= 1000
text consists of lowercase English letters and spaces.
All the words in text are separated by a single space.
1 <= first.length, second.length <= 10
first and second consist of lowercase English letters.
text will not have any leading or trailing spaces.

Complexity Analysis

  • Time Complexity:
  • Space Complexity:

1078. Occurrences After Bigram LeetCode Solution in C++

class Solution {
 public:
  vector<string> findOcurrences(string text, string first, string second) {
    vector<string> ans;
    stringstream ss(text);

    for (string prev2, prev, word; ss >> word;) {
      if (prev2 == first && prev == second)
        ans.push_back(word);
      prev2 = prev;
      prev = word;
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

1078. Occurrences After Bigram LeetCode Solution in Java

class Solution {
  public String[] findOcurrences(String text, String first, String second) {
    List<String> ans = new ArrayList<>();
    String[] words = text.split(" ");

    for (int i = 0; i + 2 < words.length; ++i)
      if (first.equals(words[i]) && second.equals(words[i + 1]))
        ans.add(words[i + 2]);

    return ans.toArray(new String[0]);
  }
}
// code provided by PROGIEZ

1078. Occurrences After Bigram LeetCode Solution in Python

class Solution:
  def findOcurrences(self, text: str, first: str, second: str) -> list[str]:
    words = text.split()
    return [c for a, b, c in zip(words, words[1:], words[2:]) if a == first and b == second]
# code by PROGIEZ

Additional Resources

See also  765. Couples Holding Hands LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.