From 4e3899e3988120aaff05922772dcb3895f08ac2a Mon Sep 17 00:00:00 2001 From: EFanZh Date: Mon, 18 Nov 2024 23:50:59 +0800 Subject: [PATCH] Add problem 2306: Naming a Company --- src/lib.rs | 1 + .../group_by_prefix.rs | 50 +++++++++++++++++++ src/problem_2306_naming_a_company/mod.rs | 24 +++++++++ 3 files changed, 75 insertions(+) create mode 100644 src/problem_2306_naming_a_company/group_by_prefix.rs create mode 100644 src/problem_2306_naming_a_company/mod.rs diff --git a/src/lib.rs b/src/lib.rs index e14b2774..0147cc3f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1717,6 +1717,7 @@ pub mod problem_2302_count_subarrays_with_score_less_than_k; pub mod problem_2303_calculate_amount_paid_in_taxes; pub mod problem_2304_minimum_path_cost_in_a_grid; pub mod problem_2305_fair_distribution_of_cookies; +pub mod problem_2306_naming_a_company; pub mod problem_2309_greatest_english_letter_in_upper_and_lower_case; pub mod problem_2310_sum_of_numbers_with_units_digit_k; pub mod problem_2315_count_asterisks; diff --git a/src/problem_2306_naming_a_company/group_by_prefix.rs b/src/problem_2306_naming_a_company/group_by_prefix.rs new file mode 100644 index 00000000..8822d8a8 --- /dev/null +++ b/src/problem_2306_naming_a_company/group_by_prefix.rs @@ -0,0 +1,50 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +use std::collections::HashSet; + +impl Solution { + pub fn distinct_names(ideas: Vec) -> i64 { + let mut groups = [(); 26].map(|()| HashSet::::new()); + + for idea in ideas { + let mut iter = idea.bytes(); + let prefix = usize::from(iter.next().unwrap()); + let suffix = iter.fold(0, |result, c| (result << 7) | u64::from(c)); + + groups[prefix - usize::from(b'a')].insert(suffix); + } + + let mut result = 0; + let mut iter = groups.iter(); + + while let Some(left) = iter.next() { + if !left.is_empty() { + iter.clone().for_each(|right| { + let intersections = left.intersection(right).count(); + + result += (left.len() - intersections) * (right.len() - intersections); + }); + } + } + + (result * 2) as _ + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn distinct_names(ideas: Vec) -> i64 { + Self::distinct_names(ideas) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_2306_naming_a_company/mod.rs b/src/problem_2306_naming_a_company/mod.rs new file mode 100644 index 00000000..7f85b4f6 --- /dev/null +++ b/src/problem_2306_naming_a_company/mod.rs @@ -0,0 +1,24 @@ +pub mod group_by_prefix; + +pub trait Solution { + fn distinct_names(ideas: Vec) -> i64; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [ + (&["coffee", "donuts", "time", "toffee"] as &[_], 6), + (&["lack", "back"], 0), + ]; + + for (ideas, expected) in test_cases { + assert_eq!( + S::distinct_names(ideas.iter().copied().map(str::to_string).collect()), + expected, + ); + } + } +}