20240817 Finished

This commit is contained in:
jackfiled 2024-08-17 11:16:11 +08:00
parent 2d191e4d62
commit 02ab7d2066
2 changed files with 43 additions and 1 deletions

View File

@ -207,3 +207,4 @@ mod p123_best_time_to_buy_and_sell_stock_iii;
mod p188_best_time_to_buy_and_sell_stock_iv; mod p188_best_time_to_buy_and_sell_stock_iv;
mod p221_maximal_square; mod p221_maximal_square;
mod p3117_minimum_sum_of_values_by_dividing_array; mod p3117_minimum_sum_of_values_by_dividing_array;
mod p3137_minimum_number_of_operations_to_make_word_k_periodic;

View File

@ -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));
}
}