20250110 finished.

This commit is contained in:
jackfiled 2025-01-10 15:05:12 +08:00
parent 6959050962
commit 9a4cee05f7
2 changed files with 69 additions and 0 deletions

View File

@ -422,3 +422,5 @@ mod p3019_number_of_changing_keys;
mod p2264_largest_3_same_digit_number_in_string; mod p2264_largest_3_same_digit_number_in_string;
mod p3297_count_substrings_that_can_be_rearranged_to_contain_a_string_i; mod p3297_count_substrings_that_can_be_rearranged_to_contain_a_string_i;
mod p3298_count_substrings_that_can_be_rearranged_to_contain_a_string_ii;

View File

@ -0,0 +1,67 @@
/**
* [3298] Count Substrings That Can Be Rearranged to Contain a String II
*/
pub struct Solution {}
// submission codes start here
use std::collections::HashMap;
impl Solution {
pub fn valid_substring_count(word1: String, word2: String) -> i64 {
let word1: Vec<char> = word1.chars().collect();
let mut word_map = HashMap::with_capacity(26);
for c in word2.chars() {
let entry = word_map.entry(c).or_insert(0);
*entry -= 1;
}
let mut result = 0;
let mut count = word_map.iter().filter(|c| c.1 < &0).count();
let mut right = 0;
for left in 0..word1.len() {
while right < word1.len() && count > 0 {
let entry = word_map.entry(word1[right]).or_insert(0);
*entry += 1;
if *entry == 0 {
count -= 1;
}
right += 1;
}
if count == 0 {
result += (word1.len() - right + 1) as i64;
}
let entry = word_map.entry(word1[left]).or_insert(0);
*entry -= 1;
if *entry == -1 {
count += 1;
}
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3298() {
assert_eq!(
1,
Solution::valid_substring_count("bcca".to_owned(), "abc".to_owned())
);
assert_eq!(
10,
Solution::valid_substring_count("abcabc".to_owned(), "abc".to_owned())
);
assert_eq!(
0,
Solution::valid_substring_count("abcabc".to_owned(), "aaabc".to_owned())
);
}
}