From 02ab7d20660915626f8949031cc690fb9a52abe6 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sat, 17 Aug 2024 11:16:11 +0800 Subject: [PATCH] 20240817 Finished --- src/problem/mod.rs | 3 +- ...r_of_operations_to_make_word_k_periodic.rs | 41 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/problem/p3137_minimum_number_of_operations_to_make_word_k_periodic.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index d1d3a79..0a6795f 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -206,4 +206,5 @@ mod p72_edit_distance; mod p123_best_time_to_buy_and_sell_stock_iii; mod p188_best_time_to_buy_and_sell_stock_iv; mod p221_maximal_square; -mod p3117_minimum_sum_of_values_by_dividing_array; \ No newline at end of file +mod p3117_minimum_sum_of_values_by_dividing_array; +mod p3137_minimum_number_of_operations_to_make_word_k_periodic; \ No newline at end of file diff --git a/src/problem/p3137_minimum_number_of_operations_to_make_word_k_periodic.rs b/src/problem/p3137_minimum_number_of_operations_to_make_word_k_periodic.rs new file mode 100644 index 0000000..d62da4e --- /dev/null +++ b/src/problem/p3137_minimum_number_of_operations_to_make_word_k_periodic.rs @@ -0,0 +1,41 @@ +/** + * [3137] Minimum Number of Operations to Make Word K-Periodic + */ +pub struct Solution {} + + +// submission codes start here +use std::collections::HashMap; + +impl Solution { + pub fn minimum_operations_to_make_k_periodic(word: String, k: i32) -> i32 { + let mut map = HashMap::new(); + let mut result = i32::MAX; + let n = word.len(); + let k = k as usize; + + for i in (0..n).step_by(k) { + let key = &word[i..i + k]; + + let entry = map.entry(key).or_insert(0); + *entry += 1; + + result = result.min((n / k) as i32 - *entry); + } + + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_3137() { + assert_eq!(1, Solution::minimum_operations_to_make_k_periodic("leetcodeleet".to_owned(), 4)); + } +}