20250610 finished.
This commit is contained in:
parent
6d9aecd427
commit
e6a52152ac
|
@ -702,3 +702,5 @@ mod p3170_lexicographically_minimum_string_after_removing_stars;
|
||||||
mod p386_lexicographical_numbers;
|
mod p386_lexicographical_numbers;
|
||||||
|
|
||||||
mod p440_k_th_smallest_in_lexicographical_order;
|
mod p440_k_th_smallest_in_lexicographical_order;
|
||||||
|
|
||||||
|
mod p3442_maximum_difference_between_even_and_odd_frequency_i;
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
/**
|
||||||
|
* [3442] Maximum Difference Between Even and Odd Frequency I
|
||||||
|
*/
|
||||||
|
pub struct Solution {}
|
||||||
|
|
||||||
|
// submission codes start here
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
pub fn max_difference(s: String) -> i32 {
|
||||||
|
let mut frequency_map = HashMap::new();
|
||||||
|
|
||||||
|
for c in s.bytes() {
|
||||||
|
let entry = frequency_map.entry(c).or_insert(0);
|
||||||
|
*entry += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut odd_max = 0;
|
||||||
|
let mut even_min = i32::MAX;
|
||||||
|
|
||||||
|
for &v in frequency_map.values() {
|
||||||
|
if v % 2 == 0 {
|
||||||
|
even_min = even_min.min(v);
|
||||||
|
} else {
|
||||||
|
odd_max = odd_max.max(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
odd_max - even_min
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// submission codes end
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_3442() {
|
||||||
|
assert_eq!(3, Solution::max_difference("aaaaabbc".to_string()));
|
||||||
|
assert_eq!(1, Solution::max_difference("abcabcab".to_string()));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user