Detect Pattern Solution
In this page, we will solve the Detect Pattern problem using multiple approaches. We will provide the implementation of the solution in Python, JavaScript, TypeScript, Java, and C++.
Problem Description​
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 elements) that occurs k
or more times within the array arr
.
The pattern can be of any length and must not overlap.
Examples​
Example 1:
Input: arr = [1,2,4,4,4,4], m = 1, k = 3
Output: true
Explanation: The repeated pattern is [4].
Example 2:
Input: arr = [1,2,1,2,1,1,1,3], m = 2, k = 2
Output: true
Explanation: The repeated pattern is [1,2].
Example 3:
Input: arr = [1,2,1,2,1,3], m = 2, k = 3
Output: false
Explanation: There is no such pattern.
Example 4:
Input: arr = [1,2,3,1,2], m = 2, k = 2
Output: false
Explanation: There is no such pattern.
Constraints​
Solution for Detect Pattern Problem​
Intuition and Approach​
We can solve this problem using a sliding window approach. We iterate through the array and check if there is a pattern of length m
that repeats k
or more times.
- Sliding Window
- Hash Map
Approach: Sliding Window​
In this approach, we use a sliding window of length m
to check for repeated patterns.
Implementation​
function detectPattern() { const arr = [1, 2, 4, 4, 4, 4]; const m = 1; const k = 3; const containsPattern = function(arr, m, k) { const n = arr.length; for (let i = 0; i <= n - m * k; i++) { let pattern = arr.slice(i, i + m); let count = 1; for (let j = i + m; j < n; j += m) { if (arr.slice(j, j + m).toString() === pattern.toString()) { count++; if (count >= k) return true; } else { break; } } } return false; }; const result = containsPattern(arr, m, k); return ( <div> <p> <b>Input:</b> arr = {JSON.stringify(arr)}, m = {m}, k = {k} </p> <p> <b>Output:</b> {result ? "true" : "false"} </p> </div> ); }
Codes in Different Languages​
- JavaScript
- TypeScript
- Python
- Java
- C++
function containsPattern(arr, m, k) {
const n = arr.length;
for (let i = 0; i <= n - m * k; i++) {
let pattern = arr.slice(i, i + m);
let count = 1;
for (let j = i + m; j < n; j += m) {
if (arr.slice(j, j + m).toString() === pattern.toString()) {
count++;
if (count >= k) return true;
} else {
break;
}
}
}
return false;
}
function containsPattern(arr: number[], m: number, k: number): boolean {
const n = arr.length;
for (let i = 0; i <= n - m * k; i++) {
let pattern = arr.slice(i, i + m);
let count = 1;
for (let j = i + m; j < n; j += m) {
if (arr.slice(j, j + m).toString() === pattern.toString()) {
count++;
if (count >= k) return true;
} else {
break;
}
}
}
return false;
}
class Solution:
def containsPattern(self, arr: List[int], m: int, k: int) -> bool:
n = len(arr)
for i in range(n - m * k + 1):
pattern = arr[i:i + m]
count = 1
for j in range(i + m, n, m):
if arr[j:j + m] == pattern:
count += 1
if count >= k:
return True
else:
break
return False
class Solution {
public boolean containsPattern(int[] arr, int m, int k) {
int n = arr.length;
for (int i = 0; i <= n - m * k; i++) {
int[] pattern = Arrays.copyOfRange(arr, i, i + m);
int count = 1;
for (int j = i + m; j < n; j += m) {
int[] subArray = Arrays.copyOfRange(arr, j, j + m);
if (Arrays.equals(pattern, subArray)) {
count++;
if (count >= k) return true;
} else {
break;
}
}
}
return false;
}
}
class Solution {
public:
bool containsPattern(vector<int>& arr, int m, int k) {
int n = arr.size();
for (int i
= 0; i <= n - m * k; i++) {
vector<int> pattern(arr.begin() + i, arr.begin() + i + m);
int count = 1;
for (int j = i + m; j < n; j += m) {
vector<int> subArray(arr.begin() + j, arr.begin() + j + m);
if (pattern == subArray) {
count++;
if (count >= k) return true;
} else {
break;
}
}
}
return false;
}
};
Complexity Analysis​
- Time Complexity:
- Space Complexity:
- Where
n
is the length of the arrayarr
andm
is the length of the pattern. - We iterate through the array once and compare patterns of length
m
. - The space complexity is constant as we use only a few variables irrespective of the size of the input.
Approach: Hash Map​
In this approach, we use a hash map to store the occurrence of patterns.
Implementation​
function detectPattern() { const arr = [1, 2, 4, 4, 4, 4]; const m = 1; const k = 3; const containsPattern = function(arr, m, k) { const patternMap = new Map(); for (let i = 0; i <= arr.length - m * k; i++) { let pattern = arr.slice(i, i + m).toString(); if (!patternMap.has(pattern)) { patternMap.set(pattern, 1); } else { patternMap.set(pattern, patternMap.get(pattern) + 1); } if (patternMap.get(pattern) >= k) return true; } return false; }; const result = containsPattern(arr, m, k); return ( <div> <p> <b>Input:</b> arr = {JSON.stringify(arr)}, m = {m}, k = {k} </p> <p> <b>Output:</b> {result ? "true" : "false"} </p> </div> ); }
Codes in Different Languages​
- JavaScript
- TypeScript
- Python
- Java
- C++
function containsPattern(arr, m, k) {
const patternMap = new Map();
for (let i = 0; i <= arr.length - m * k; i++) {
let pattern = arr.slice(i, i + m).toString();
if (!patternMap.has(pattern)) {
patternMap.set(pattern, 1);
} else {
patternMap.set(pattern, patternMap.get(pattern) + 1);
}
if (patternMap.get(pattern) >= k) return true;
}
return false;
}
function containsPattern(arr: number[], m: number, k: number): boolean {
const patternMap = new Map();
for (let i = 0; i <= arr.length - m * k; i++) {
let pattern = arr.slice(i, i + m).toString();
if (!patternMap.has(pattern)) {
patternMap.set(pattern, 1);
} else {
patternMap.set(pattern, patternMap.get(pattern) + 1);
}
if (patternMap.get(pattern) >= k) return true;
}
return false;
}
class Solution:
def containsPattern(self, arr: List[int], m: int, k: int) -> bool:
pattern_map = {}
for i in range(len(arr) - m * k + 1):
pattern = tuple(arr[i:i + m])
pattern_map[pattern] = pattern_map.get(pattern, 0) + 1
if pattern_map[pattern] >= k:
return True
return False
class Solution {
public boolean containsPattern(int[] arr, int m, int k) {
Map<String, Integer> patternMap = new HashMap<>();
for (int i = 0; i <= arr.length - m * k; i++) {
String pattern = Arrays.toString(Arrays.copyOfRange(arr, i, i + m));
patternMap.put(pattern, patternMap.getOrDefault(pattern, 0) + 1);
if (patternMap.get(pattern) >= k) return true;
}
return false;
}
}
class Solution {
public:
bool containsPattern(vector<int>& arr, int m, int k) {
unordered_map<string, int> patternMap;
for (int i = 0; i <= arr.size() - m * k; i++) {
string pattern = "";
for (int j = i; j < i + m; j++) {
pattern += to_string(arr[j]) + ",";
}
patternMap[pattern]++;
if (patternMap[pattern] >= k) return true;
}
return false;
}
};
Complexity Analysis​
- Time Complexity:
- Space Complexity:
- Where
n
is the length of the arrayarr
andm
is the length of the pattern. - We iterate through the array once and store patterns in the hash map.
- The space complexity is proportional to the number of distinct patterns.
By using these approaches, we can efficiently solve the Detect Pattern problem for the given constraints.
References​
- LeetCode Problem: LeetCode Problem
- Solution Link: Detect Pattern Solution on LeetCode
- Authors LeetCode Profile: Manish Kumar Gupta