← Home

1078. Occurrences After Bigram

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
107

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 1078. Occurrences After Bigram, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are string_split, findOcurrences.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • Substring checks are convenient but not free, so they are part of the real complexity story.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Occurrences After Bigram.
02//Memory Usage: 8.8 MB, less than 100.00% of C++ online submissions for Occurrences After Bigram.
03class Solution {
04public:
05    std::vector<std::string> string_split(std::string str, std::string delimiter){
06        size_t pos = 0;
07        std::string token;
08        std::vector<std::string> result;
09        while ((pos = str.find(delimiter)) != std::string::npos) {
10            token = str.substr(0, pos);
11            result.push_back(token);
12            str.erase(0, pos + delimiter.length());
13        }
14        result.push_back(str);
15        return result;
16    }
17    
18    vector<string> findOcurrences(string text, string first, string second) {
19        vector<string> split = string_split(text, " ");
20        vector<string> ans;
21        
22        for(int i = 0; i < split.size() - 1; i++){
23            if(split[i] == first && split[i+1] == second && i+2 < split.size()){
24                ans.push_back(split[i+2]);
25            }
26        }
27        return ans;
28    }
29};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.