aboutsummaryrefslogtreecommitdiff
path: root/detect_pattern_len_m.py
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-08-30 22:11:11 -0600
committerJack Sangdahl <[email protected]>2024-08-30 22:11:11 -0600
commit48c78e040acbb45dd3f60bc948b5c1c26a4dfd26 (patch)
treee542ef5f091f847cc7400aa10cfb8e7588db89ee /detect_pattern_len_m.py
parentf41e4f99df5fad156487150cc827980d339281a8 (diff)
downloadleetcode-48c78e040acbb45dd3f60bc948b5c1c26a4dfd26.tar.gz
leetcode-48c78e040acbb45dd3f60bc948b5c1c26a4dfd26.zip
1566. detect pattern of length m repeated k or more times
Diffstat (limited to 'detect_pattern_len_m.py')
-rw-r--r--detect_pattern_len_m.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/detect_pattern_len_m.py b/detect_pattern_len_m.py
new file mode 100644
index 0000000..ac4adfe
--- /dev/null
+++ b/detect_pattern_len_m.py
@@ -0,0 +1,33 @@
+# 1566. 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
+subsequence) 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
+"""
+
+
+class Solution(object):
+ def containsPattern(self, arr, m, k):
+ """
+ :type arr: List[int]
+ :type m: int
+ :type k: int
+ :rtype: bool
+ """
+ n = 0
+ for i in range(len(arr) - m):
+ n = n + 1 if arr[i] == arr[i + m] else 0
+ if n == (k - 1) * m:
+ return True
+ return False
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.containsPattern(arr=[1, 2, 4, 4, 4, 4], m=1, k=3))
+ print(obj.containsPattern(arr=[1, 2, 1, 2, 1, 1, 1, 3], m=2, k=2))
+ print(obj.containsPattern(arr=[1, 2, 1, 2, 1, 3], m=2, k=3))