1566. Detect Pattern of Length M Repeated K or More Times LeetCode Solution
In this guide, you will get 1566. Detect Pattern of Length M Repeated K or More Times LeetCode Solution with the best time and space complexity. The solution to Detect Pattern of Length M Repeated K or More Times problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Detect Pattern of Length M Repeated K or More Times solution in C++
- Detect Pattern of Length M Repeated K or More Times solution in Java
- Detect Pattern of Length M Repeated K or More Times solution in Python
- Additional Resources
Problem Statement of Detect Pattern of Length M Repeated K or More Times
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. A pattern is defined by its length and the number of repetitions.
Return true if there exists a pattern of length m that is repeated k or more times, otherwise return false.
Example 1:
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.
Example 2:
Input: arr = [1,2,1,2,1,1,1,3], m = 2, k = 2
Output: true
Explanation: The pattern (1,2) of length 2 is repeated 2 consecutive times. Another valid pattern (2,1) is also repeated 2 times.
Example 3:
Input: arr = [1,2,1,2,1,3], m = 2, k = 3
Output: false
Explanation: The pattern (1,2) is of length 2 but is repeated only 2 times. There is no pattern of length 2 that is repeated 3 or more times.
Constraints:
2 <= arr.length <= 100
1 <= arr[i] <= 100
1 <= m <= 100
2 <= k <= 100
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
1566. Detect Pattern of Length M Repeated K or More Times LeetCode Solution in C++
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;
}
};
/* code provided by PROGIEZ */
1566. Detect Pattern of Length M Repeated K or More Times LeetCode Solution in Java
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;
}
}
// code provided by PROGIEZ
1566. Detect Pattern of Length M Repeated K or More Times LeetCode Solution in Python
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
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.