Detect Repeated Patterns in a String Sequence
Given a sequence of strings, determine whether some contiguous block of pattern_length items repeats consecutively at least repeat_count times.
Function Signature
contains_repeated_pattern(items: list[str], pattern_length: int, repeat_count: int) -> bool
Valid Input Domain
items contains case-sensitive strings. pattern_length is positive, and repeat_count is at least two.
Exact Output Semantics
Return true when there is a start index such that the next pattern_length * repeat_count items consist of repeat_count equal adjacent blocks, each of length pattern_length. A longer run also qualifies because it contains at least the required number of copies. Return false otherwise.
Constraints
-
0 <= items.length <= 200,000
.
-
1 <= pattern_length <= 200,000
.
-
2 <= repeat_count <= 200,000
.
-
Each string has length from 0 through 100.
Public Examples
Example 1
Input: items = ["a", "b", "a", "b", "a", "b"], pattern_length = 2, repeat_count = 2
Output: true
The block ["a", "b"] repeats three times, which is at least the two required copies.
Example 2
Input: items = ["a", "b", "a", "c"], pattern_length = 2, repeat_count = 2
Output: false
The second two-item block does not equal the first.
Hints
-
Compare each item in a candidate repeated region with the item one block length earlier.
-
Stop considering a start as soon as one aligned pair differs.