|
| 1 | +/** |
| 2 | + * [1566] Detect Pattern of Length M Repeated K or More Times |
| 3 | + * |
| 4 | + * Given an array of positive integers arr, find a pattern of length m that is repeated k or more times. |
| 5 | + * 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. |
| 6 | + * Return true if there exists a pattern of length m that is repeated k or more times, otherwise return false. |
| 7 | + * |
| 8 | + * Example 1: |
| 9 | + * |
| 10 | + * Input: arr = [1,2,4,4,4,4], m = 1, k = 3 |
| 11 | + * Output: true |
| 12 | + * 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. |
| 13 | + * |
| 14 | + * Example 2: |
| 15 | + * |
| 16 | + * Input: arr = [1,2,1,2,1,1,1,3], m = 2, k = 2 |
| 17 | + * Output: true |
| 18 | + * Explanation: The pattern (1,2) of length 2 is repeated 2 consecutive times. Another valid pattern (2,1) is also repeated 2 times. |
| 19 | + * |
| 20 | + * Example 3: |
| 21 | + * |
| 22 | + * Input: arr = [1,2,1,2,1,3], m = 2, k = 3 |
| 23 | + * Output: false |
| 24 | + * 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. |
| 25 | + * |
| 26 | + * |
| 27 | + * Constraints: |
| 28 | + * |
| 29 | + * 2 <= arr.length <= 100 |
| 30 | + * 1 <= arr[i] <= 100 |
| 31 | + * 1 <= m <= 100 |
| 32 | + * 2 <= k <= 100 |
| 33 | + * |
| 34 | + */ |
| 35 | +pub struct Solution {} |
| 36 | + |
| 37 | +// problem: https://leetcode.com/problems/detect-pattern-of-length-m-repeated-k-or-more-times/ |
| 38 | +// discuss: https://leetcode.com/problems/detect-pattern-of-length-m-repeated-k-or-more-times/discuss/?currentPage=1&orderBy=most_votes&query= |
| 39 | + |
| 40 | +// submission codes start here |
| 41 | + |
| 42 | +impl Solution { |
| 43 | + pub fn contains_pattern(arr: Vec<i32>, m: i32, k: i32) -> bool { |
| 44 | + let m = m as usize; |
| 45 | + |
| 46 | + for i in 0..=arr.len() - m { |
| 47 | + let pattern = &arr[i..i + m]; |
| 48 | + let count = arr[i + m..] |
| 49 | + .chunks(m) |
| 50 | + .take_while(|&another_pattern| pattern == another_pattern) |
| 51 | + .count() as i32; |
| 52 | + if count >= k - 1 { |
| 53 | + return true; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + false |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// submission codes end |
| 62 | + |
| 63 | +#[cfg(test)] |
| 64 | +mod tests { |
| 65 | + use super::*; |
| 66 | + |
| 67 | + #[test] |
| 68 | + fn test_1566_example_1() { |
| 69 | + let arr = vec![1, 2, 4, 4, 4, 4]; |
| 70 | + let m = 1; |
| 71 | + let k = 3; |
| 72 | + |
| 73 | + let result = true; |
| 74 | + |
| 75 | + assert_eq!(Solution::contains_pattern(arr, m, k), result); |
| 76 | + } |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn test_1566_example_2() { |
| 80 | + let arr = vec![1, 2, 1, 2, 1, 1, 1, 3]; |
| 81 | + let m = 2; |
| 82 | + let k = 2; |
| 83 | + |
| 84 | + let result = true; |
| 85 | + |
| 86 | + assert_eq!(Solution::contains_pattern(arr, m, k), result); |
| 87 | + } |
| 88 | + |
| 89 | + #[test] |
| 90 | + fn test_1566_example_3() { |
| 91 | + let arr = vec![1, 2, 1, 2, 1, 3]; |
| 92 | + let m = 2; |
| 93 | + let k = 3; |
| 94 | + |
| 95 | + let result = false; |
| 96 | + |
| 97 | + assert_eq!(Solution::contains_pattern(arr, m, k), result); |
| 98 | + } |
| 99 | +} |
0 commit comments