Algorithms to Detect Pattern of Length M Repeated K or More Time
- Time:2020-09-07 12:03:44
- Class:Weblog
- Read:36
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.Example 4:
Input: arr = [1,2,3,1,2], m = 2, k = 2
Output: false
Explanation: Notice that the pattern (1,2) exists twice but not consecutively, so it doesn’t count.Example 5:
Input: arr = [2,2,2,2], m = 2, k = 3
Output: false
Explanation: The only pattern of length 2 is (2,2) however it’s repeated only twice. Notice that we do not count overlapping repetitions.Constraints:
2 <= arr.length <= 100
1 <= arr[i] <= 100
1 <= m <= 100
2 <= k <= 100
Bruteforce Algorithm to Determine the Repeative String Pattern
Given the m and k are only less than 100 – we can totally do bruteforce algorithm. First loop the start index of the first pattern – then, we know the first pattern looks like – and we can construct k such patterns – and see if it equals to the sub array of the string.
The following bruteforce implemented in Python has O(N^2) time complexity.
1 2 3 4 5 6 7 8 | class Solution: def containsPattern(self, arr: List[int], m: int, k: int) -> bool: L = len(arr) for i in range(L - m * k + 1): p = arr[i:i+m]*k if p == arr[i:i+m*k]: return True return False |
class Solution: def containsPattern(self, arr: List[int], m: int, k: int) -> bool: L = len(arr) for i in range(L - m * k + 1): p = arr[i:i+m]*k if p == arr[i:i+m*k]: return True return False
Better Bruteforce Algorithm to Detect Pattern of Length M Repeated K or More Times
We can do this in a better way. We can check if the value at index i and index i + m equals – and increment the counter. If the counter equals (k-1)*m then we have k times of m-size pattern.
1 2 3 4 5 6 7 8 9 10 11 12 | class Solution: def containsPattern(self, arr: List[int], m: int, k: int) -> bool: L = len(arr) cnt = 0 for i in range(L - m): if arr[i] == arr[i + m]: cnt += 1 else: cnt = 0 if cnt == m * (k - 1): return True return False |
class Solution: def containsPattern(self, arr: List[int], m: int, k: int) -> bool: L = len(arr) cnt = 0 for i in range(L - m): if arr[i] == arr[i + m]: cnt += 1 else: cnt = 0 if cnt == m * (k - 1): return True return False
The complexity is O(N).
–EOF (The Ultimate Computing & Technology Blog) —
Recommend:Algorithms to Count How Many Numbers Are Smaller Than the Curren
Finding the Closest Divisors
Greedy Solution to Reconstruct a 2-Row Binary Matrix
How to Use jOOQ Library to Write SQL in Java using Fluent Style?
Algorithm to Compute the Number of Days Between Two Dates
How to Sort Integers by The Number of 1 Bits?
Using Depth First Search Algorithm to Delete Tree Nodes with Sum
Web Strategies to Start Your SEO Journey
How to Compute the Product of Last K elements in Array using the
How to Write a High-Quality Blog Post in Just 30 Minutes
- Comment list
-
- Comment add