From 690731caaa14b36d24542acc0b66bcf365ce19e2 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Fri, 20 Dec 2024 11:14:51 +0800 Subject: [PATCH] 20241220 finished. --- src/problem/mod.rs | 2 + ...minimum_length_of_anagram_concatenation.rs | 61 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/problem/p3138_minimum_length_of_anagram_concatenation.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 4c2789d..2f6c3e6 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -380,3 +380,5 @@ mod p3291_minimum_number_of_valid_strings_to_form_target_i; mod p3292_minimum_number_of_valid_strings_to_form_target_ii; mod p3285_find_indices_of_stable_mountains; + +mod p3138_minimum_length_of_anagram_concatenation; diff --git a/src/problem/p3138_minimum_length_of_anagram_concatenation.rs b/src/problem/p3138_minimum_length_of_anagram_concatenation.rs new file mode 100644 index 0000000..3c6a3ea --- /dev/null +++ b/src/problem/p3138_minimum_length_of_anagram_concatenation.rs @@ -0,0 +1,61 @@ +/** + * [3138] Minimum Length of Anagram Concatenation + */ +pub struct Solution {} + +// submission codes start here +use std::collections::HashMap; + +impl Solution { + pub fn min_anagram_length(s: String) -> i32 { + let s: Vec = s.chars().collect(); + let length = s.len(); + + for i in 1..length { + if length % i != 0 { + continue; + } + + let count = length / i; + let mut standard_map = HashMap::new(); + + for k in 0..i { + let entry = standard_map.entry(s[k]).or_insert(0); + *entry += 1; + } + + let mut flag = true; + for j in 1..count { + let mut map = HashMap::new(); + for k in (j * i)..(i * (j + 1)) { + let entry = map.entry(s[k]).or_insert(0); + *entry += 1; + } + + flag = map == standard_map; + if !flag { + break; + } + } + + if flag { + return i as i32; + } + } + + s.len() as i32 + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_3138() { + assert_eq!(2, Solution::min_anagram_length("abba".to_owned())); + assert_eq!(4, Solution::min_anagram_length("cdef".to_owned())); + } +}