From 9f09445dab5d9edf51e5de3e930f776a8b206c7c Mon Sep 17 00:00:00 2001 From: jackfiled Date: Thu, 13 Mar 2025 14:14:13 +0800 Subject: [PATCH] 20250313 finished. --- src/problem/mod.rs | 2 + ...taining_every_vowel_and_k_consonants_ii.rs | 63 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 src/problem/p3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 9173453..c1ddddd 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -534,3 +534,5 @@ mod p2269_find_the_k_beauty_of_a_number; mod p2012_sum_of_beauty_in_the_array; mod p3305_count_of_substrings_containing_every_vowel_and_k_consonants_i; + +mod p3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii; diff --git a/src/problem/p3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii.rs b/src/problem/p3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii.rs new file mode 100644 index 0000000..ef5604f --- /dev/null +++ b/src/problem/p3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii.rs @@ -0,0 +1,63 @@ +/** + * [3306] Count of Substrings Containing Every Vowel and K Consonants II + */ +pub struct Solution {} + +// submission codes start here +use std::collections::{HashMap, HashSet}; + +impl Solution { + pub fn count_of_substrings(word: String, k: i32) -> i64 { + let word: Vec = word.chars().collect(); + let vowel_set = HashSet::from(['a', 'e', 'i', 'o', 'u']); + + let count = |m| -> i64 { + let mut consonants = 0; + let mut result = 0; + let mut occurence = HashMap::new(); + + let mut right = 0; + for left in 0..word.len() { + while right < word.len() && (consonants < m || occurence.len() < vowel_set.len()) { + if vowel_set.contains(&word[right]) { + let entry = occurence.entry(word[right]).or_insert(0i64); + *entry += 1; + } else { + consonants += 1; + } + right += 1; + } + + if consonants >= m && occurence.len() == vowel_set.len() { + result += (word.len() - right + 1) as i64; + } + + if vowel_set.contains(&word[left]) { + let v = occurence.get_mut(&word[left]).unwrap(); + *v -= 1; + if *v == 0 { + occurence.remove(&word[left]); + } + } else { + consonants -= 1; + } + } + + result + }; + + count(k) - count(k + 1) + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_3306() { + assert_eq!(0, Solution::count_of_substrings("aeioqq".to_owned(), 1)); + } +}