From f41a9f08271f453269c7b4cf6642195938b10959 Mon Sep 17 00:00:00 2001 From: Ivan Date: Wed, 7 Feb 2024 17:55:27 +0500 Subject: [PATCH] leetcode: solve problem 929 --- .../0929-unique-email-addresses/Cargo.lock | 7 +++++ .../0929-unique-email-addresses/Cargo.toml | 8 +++++ .../0929-unique-email-addresses/src/main.rs | 29 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 leetcode/0929-unique-email-addresses/Cargo.lock create mode 100644 leetcode/0929-unique-email-addresses/Cargo.toml create mode 100644 leetcode/0929-unique-email-addresses/src/main.rs diff --git a/leetcode/0929-unique-email-addresses/Cargo.lock b/leetcode/0929-unique-email-addresses/Cargo.lock new file mode 100644 index 0000000..559771a --- /dev/null +++ b/leetcode/0929-unique-email-addresses/Cargo.lock @@ -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" diff --git a/leetcode/0929-unique-email-addresses/Cargo.toml b/leetcode/0929-unique-email-addresses/Cargo.toml new file mode 100644 index 0000000..be25eb0 --- /dev/null +++ b/leetcode/0929-unique-email-addresses/Cargo.toml @@ -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] diff --git a/leetcode/0929-unique-email-addresses/src/main.rs b/leetcode/0929-unique-email-addresses/src/main.rs new file mode 100644 index 0000000..fd212a0 --- /dev/null +++ b/leetcode/0929-unique-email-addresses/src/main.rs @@ -0,0 +1,29 @@ +use std::collections::HashSet; + +pub fn num_unique_emails(emails: Vec) -> i32 { + let mut unique_emails = HashSet::new(); + + for email in emails.iter() { + let mut parts: Vec = email + .split("@") + .map(|s| s.to_string()) + .collect(); + parts[0] = parts[0] + .replace(".", "") + .split("+") + .collect::>()[0] + .to_string(); + unique_emails.insert(format!("{}@{}", parts[0], parts[1])); + } + + unique_emails.len() as i32 +} + +fn main() { + let inp: Vec = 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)); +}