Skip to content

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

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
 public:
  bool containsPattern(vector<int>& arr, int m, int k) {
    int count = 0;
    for (int i = m; i < arr.size(); ++i) {
      count = arr[i] == arr[i - m] ? count + 1 : 0;
      if (count == m * k - m)
        return true;
    }
    return false;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
  public boolean containsPattern(int[] arr, int m, int k) {
    int count = 0;
    for (int i = m; i < arr.length; ++i) {
      count = arr[i] == arr[i - m] ? count + 1 : 0;
      if (count == m * k - m)
        return true;
    }
    return false;
  }
}
1
2
3
4
5
6
7
8
class Solution:
  def containsPattern(self, arr: list[int], m: int, k: int) -> bool:
    count = 0
    for i in range(m, len(arr)):
      count = count + 1 if arr[i] == arr[i - m] else 0
      if count == m * k - m:
        return True
    return False