Algorithms to Detect Pattern of Length M Repeated K or More Time
- Time:2020-09-07 12:03:44
- Class:Weblog
- Read:30
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:Want to Generate More Engagement on Twitter?
4 Handy Tips for Bloggers Exploring a New Industry
3 Ambitious Blog Subjects for Small Businesses
How to Write Fresh Blog Content After Years on the Job
5 Clean Energy Blogs to Read in 2019
Social Media is Dying. Here are 3 Ways to Hack it for More Traff
5 Skills All Successful Content Writers Have Mastered
Blogging for Dummies in 2019: The Only Cheat Sheet You Need
Are Readers Reaching Your Call To Action?
How to Manage a Content Marketing Plan for Your Blog
- Comment list
-
- Comment add