File: detect-pattern-of-length-m-repeated-k-or-more-times/solution.py

Date: 2026-06-06

Time: 16:20

Purpose

This file solves LeetCode 1566: Detect Pattern of Length M Repeated K or More Times. It determines whether any contiguous subarray of length m appears k or more times consecutively in the input array. It's a standalone solution module following the repo's convention of one problem per directory.

Key Components

contains_pattern(arr, m, k) -> bool — The sole function. It takes an integer array, a pattern length m, and a repetition count k, returning True if any length-m subarray repeats k+ times back-to-back.

The contract is straightforward: given valid inputs per the LeetCode constraints (1 <= m*k <= len(arr)), it returns a boolean. No mutation of inputs.

Patterns

Brute-force enumeration with slice comparison. The function tries every possible starting index i as the beginning of a repeating pattern, extracts the candidate pattern = arr[i:i+m], then checks whether the next k-1 consecutive blocks of length m are identical via all() with a generator.

This is idiomatic Python — using list slicing for pattern extraction and all() for short-circuit evaluation. No need for hashing or more complex string-matching algorithms given the problem's small constraints (array length <= 100).

Dependencies

Imports: Only typing.List for the type annotation — no external dependencies.

Imported by: The massive Imported By list is misleading — it reflects test files across the entire repo importing from a shared test harness or common structure, not direct imports of containspattern. The actual consumer is detect-pattern-of-length-m-repeated-k-or-more-times/testsolution.py.

Flow

1. Compute n = len(arr).

2. Iterate i from 0 to n - m*k inclusive — this is the last valid start index where k repetitions of length m could fit.

3. Extract pattern = arr[i:i+m].

4. For each j in range(1, k), compare arr[i + j*m : i + j*m + m] against pattern.

5. all() short-circuits on the first mismatch. If all k-1 subsequent blocks match, return True.

6. If no starting index yields a match, return False.

The outer loop runs at most n - m*k + 1 iterations. The inner generator runs at most k-1 comparisons, each comparing m elements. Worst case: O(n * m * k), which is fine for n <= 100.

Invariants

Error Handling

None. The function trusts its caller to provide valid inputs per LeetCode constraints. If m * k > n, the range in the outer loop is empty and the function returns False — a graceful no-op rather than an error.

Topics to Explore

Beliefs