leetcode: solve problem 929

This commit is contained in:
Ivan R. 2024-02-07 17:55:27 +05:00
parent dea3af5d33
commit f41a9f0827
Signed by: lumin
GPG key ID: 927D3132247BDE4E
3 changed files with 44 additions and 0 deletions

View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "unique-email-addresses"
version = "0.1.0"

View file

@ -0,0 +1,8 @@
[package]
name = "unique-email-addresses"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View file

@ -0,0 +1,29 @@
use std::collections::HashSet;
pub fn num_unique_emails(emails: Vec<String>) -> i32 {
let mut unique_emails = HashSet::new();
for email in emails.iter() {
let mut parts: Vec<String> = email
.split("@")
.map(|s| s.to_string())
.collect();
parts[0] = parts[0]
.replace(".", "")
.split("+")
.collect::<Vec<&str>>()[0]
.to_string();
unique_emails.insert(format!("{}@{}", parts[0], parts[1]));
}
unique_emails.len() as i32
}
fn main() {
let inp: Vec<String> = vec![
"test.email+alex@leetcode.com",
"test.e.mail+bob.cathy@leetcode.com",
"testemail+david@lee.tcode.com",
].iter().map(|s| s.to_string()).collect();
println!("{}", num_unique_emails(inp));
}