← Home

1566. Detect Pattern of Length M Repeated K or More Times

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

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 1566. Detect Pattern of Length M Repeated K or More Times, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. 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.

The notes already sitting in the source point us in the right direction:

  • Brute force
  • time: O(N^3)

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are containsPattern, pattern.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • 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. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

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)
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Brute force
02//Runtime: 4 ms, faster than 50.00% of C++ online submissions for Detect Pattern of Length M Repeated K or More Times.
03//Memory Usage: 8.8 MB, less than 25.00% of C++ online submissions for Detect Pattern of Length M Repeated K or More Times.
04//time: O(N^3)
05class Solution {
06public:
07    bool containsPattern(vector<int>& arr, int m, int k) {
08        int n = arr.size();
09        
10        for(int i = 0; i+m*k <= n; ++i){
11            vector<int> pattern(arr.begin()+i, arr.begin()+i+m);
12            bool found = true;
13            for(int t = 1; t < k; ++t){
14                // cout << "[" << i+m*t << ", " << i+m*(t+1) << ")" << endl;
15                if(pattern != vector<int>(arr.begin()+i+m*t, arr.begin()+i+m*(t+1))){
16                    found = false;
17                    break;
18                }
19            }
20            // if(found){
21            //     for(int e : pattern){
22            //         cout << e << " ";
23            //     }
24            //     cout << endl;
25            // }
26            if(found) return true;
27        }
28        
29        return false;
30    }
31};
32
33//One Pass
34//https://leetcode.com/problems/detect-pattern-of-length-m-repeated-k-or-more-times/discuss/819361/Simple-c%2B%2B-solution(0ms)-100-fast
35//time: O(N)
36class Solution {
37public:
38    bool containsPattern(vector<int>& arr, int m, int k) {
39        int cnt = 0;
40        int n = arr.size();
41        
42        for(int i = 0; i+m < n; ++i){
43            if(arr[i] != arr[i+m]){
44                cnt = 0;
45            }else{
46                ++cnt;
47            }
48            
49            if(cnt == (k-1)*m)
50                return true;
51        }
52        
53        return false;
54    }
55};

Cost

Complexity

Time
O(N)
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.