20240925 finished.

This commit is contained in:
jackfiled 2024-09-25 12:10:24 +08:00
parent ed478339df
commit 1f20dc3d7f
2 changed files with 48 additions and 1 deletions

View File

@ -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;
mod p2207_maximize_number_of_subsequences_in_a_string;
mod p2306_naming_a_company;

View File

@ -0,0 +1,46 @@
/**
* [2306] Naming a Company
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn distinct_names(ideas: Vec<String>) -> 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<HashSet<&str>> = 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")));
}
}