20240828 finished.

This commit is contained in:
jackfiled 2024-08-28 13:21:19 +08:00
parent 0f1557c7bd
commit e9eeb2d008
2 changed files with 51 additions and 1 deletions

View File

@ -217,4 +217,5 @@ mod p3133_minimum_array_end;
mod p3145_find_products_of_elements_of_big_array;
mod p3146_permutation_difference_between_two_strings;
mod p698_partition_to_k_equal_sum_subsets;
mod p3134_find_the_median_of_the_uniqueness_array;
mod p3134_find_the_median_of_the_uniqueness_array;
mod p3144_minimum_substring_partition_of_equal_character_frequency;

View File

@ -0,0 +1,49 @@
/**
* [3144] Minimum Substring Partition of Equal Character Frequency
*/
pub struct Solution {}
// submission codes start here
use std::collections::HashMap;
impl Solution {
pub fn minimum_substrings_in_partition(s: String) -> i32 {
let s: Vec<char> = s.chars().collect();
let n = s.len();
let mut dp = vec![i32::MAX;n + 1];
let mut char_count = HashMap::new();
dp[0] = 0;
for i in 1..=n {
let mut max_count = 0;
char_count.clear();
for j in (1..=i).rev() {
let entry = char_count.entry(s[j - 1]).or_insert(0);
*entry += 1;
max_count = max_count.max(*entry);
if max_count * char_count.len() == i - j + 1 && dp[j - 1] != i32::MAX {
dp[i] = dp[i].min(dp[j - 1] + 1);
}
}
}
dp[n]
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3144() {
assert_eq!(3, Solution::minimum_substrings_in_partition("fabccddg".to_owned()));
assert_eq!(2, Solution::minimum_substrings_in_partition("abababaccddb".to_owned()));
}
}