Leetcode #1566: Detect Pattern of Length M Repeated K or More Times
In this guide, we solve Leetcode #1566 Detect Pattern of Length M Repeated K or More Times in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Problem Statement
Given an array of positive integers arr, find a pattern of length m that is repeated k or more times. A pattern is a subarray (consecutive sub-sequence) that consists of one or more values, repeated multiple times consecutively without overlapping.
Quick Facts
- Difficulty: Easy
- Premium: No
- Tags: Array, Enumeration
Intuition
The constraints allow a direct scan that keeps only the essential state.
By translating the requirements into a clean loop, the logic stays easy to reason about.
Approach
Iterate through the data once, updating the state needed to compute the answer.
Return the final state after the traversal is complete.
Steps:
- Parse the input.
- Iterate and update state.
- Return the computed answer.
Example
Input: arr = [1,2,4,4,4,4], m = 1, k = 3
Output: true
Explanation: The pattern (4) of length 1 is repeated 4 consecutive times. Notice that pattern can be repeated k or more times but not less.
Python Solution
class Solution:
def containsPattern(self, arr: List[int], m: int, k: int) -> bool:
if len(arr) < m * k:
return False
cnt, target = 0, (k - 1) * m
for i in range(m, len(arr)):
if arr[i] == arr[i - m]:
cnt += 1
if cnt == target:
return True
else:
cnt = 0
return False
Complexity
The time complexity is , where is the length of the array. The space complexity is .
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.