Skip to content

Commit c38f9f2

Browse files
committed
Added solution of problem 1566
1 parent fbb5db0 commit c38f9f2

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/solutions/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ pub mod s1544_make_the_string_great;
332332
pub mod s1550_three_consecutive_odds;
333333
pub mod s1556_thousand_separator;
334334
pub mod s1560_most_visited_sector_in_a_circular_track;
335+
pub mod s1566_detect_pattern_of_length_m_repeated_k_or_more_times;
335336
pub mod s1619_mean_of_array_after_removing_some_elements;
336337
pub mod s1935_maximum_number_of_words_you_can_type;
337338
pub mod s1957_delete_characters_to_make_fancy_string;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
pub fn contains_pattern(arr: Vec<i32>, m: i32, k: i32) -> bool {
2+
let mut result: bool = false;
3+
let mut count: i32 = 0;
4+
5+
if arr.len() < (m * k) as usize {
6+
return result;
7+
}
8+
9+
'main: for i in 0..=(arr.len() - (m * k) as usize) {
10+
let mut prev = None;
11+
for subset in arr[i..].chunks_exact(m as usize) {
12+
let current = Some(subset);
13+
match prev == current {
14+
true => count += 1,
15+
false => count = 0,
16+
}
17+
18+
if count + 1 == k {
19+
result = true;
20+
break 'main;
21+
}
22+
prev = current;
23+
}
24+
}
25+
26+
result
27+
}
28+
29+
#[cfg(test)]
30+
mod test {
31+
use super::*;
32+
33+
#[test]
34+
fn test_case_1() {
35+
assert_eq!(contains_pattern(vec![1, 2, 4, 4, 4, 4], 1, 3), true);
36+
}
37+
38+
#[test]
39+
fn test_case_2() {
40+
assert_eq!(contains_pattern(vec![1, 2, 1, 2, 1, 1, 1, 3], 2, 2), true);
41+
}
42+
43+
#[test]
44+
fn test_case_3() {
45+
assert_eq!(contains_pattern(vec![1, 2, 1, 2, 1, 3], 2, 3), false);
46+
}
47+
}

0 commit comments

Comments
 (0)