From 1f20dc3d7f7acccf543d5fac88ab67cb4f1d972c Mon Sep 17 00:00:00 2001 From: jackfiled Date: Wed, 25 Sep 2024 12:10:24 +0800 Subject: [PATCH] 20240925 finished. --- src/problem/mod.rs | 3 +- src/problem/p2306_naming_a_company.rs | 46 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/problem/p2306_naming_a_company.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index d3d4a37..cdcb39c 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -244,4 +244,5 @@ mod p2414_length_of_the_longest_alphabetical_continuous_substring; mod p2376_count_special_integers; mod p2374_node_with_highest_edge_score; mod p997_find_the_town_judge; -mod p2207_maximize_number_of_subsequences_in_a_string; \ No newline at end of file +mod p2207_maximize_number_of_subsequences_in_a_string; +mod p2306_naming_a_company; \ No newline at end of file diff --git a/src/problem/p2306_naming_a_company.rs b/src/problem/p2306_naming_a_company.rs new file mode 100644 index 0000000..66a72b7 --- /dev/null +++ b/src/problem/p2306_naming_a_company.rs @@ -0,0 +1,46 @@ +/** + * [2306] Naming a Company + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn distinct_names(ideas: Vec) -> i64 { + use std::collections::{HashSet, HashMap}; + + let mut map = HashMap::new(); + + for idea in ideas.iter() { + let entry = map.entry(&idea[..1]).or_insert(HashSet::new()); + entry.insert(&idea[1..]); + } + + let mut result = 0; + let values: Vec> = map.into_iter().map(|p| p.1).collect(); + + for i in 0..values.len() { + for j in i + 1..values.len() { + let intersect = values[i].intersection(&values[j]).count(); + + result += (values[i].len() - intersect) as i64 * (values[j].len() - intersect) as i64 + } + } + + result * 2 + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2306() { + assert_eq!(6, Solution::distinct_names(vec_string!("coffee", "donuts", "time", "toffee"))); + assert_eq!(0, Solution::distinct_names(vec_string!("lack", "back"))); + } +}