leetcode: solve problem 929
This commit is contained in:
parent
dea3af5d33
commit
f41a9f0827
3 changed files with 44 additions and 0 deletions
7
leetcode/0929-unique-email-addresses/Cargo.lock
generated
Normal file
7
leetcode/0929-unique-email-addresses/Cargo.lock
generated
Normal 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"
|
8
leetcode/0929-unique-email-addresses/Cargo.toml
Normal file
8
leetcode/0929-unique-email-addresses/Cargo.toml
Normal 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]
|
29
leetcode/0929-unique-email-addresses/src/main.rs
Normal file
29
leetcode/0929-unique-email-addresses/src/main.rs
Normal 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));
|
||||
}
|
Loading…
Reference in a new issue