Skip to content

Commit 97bd66f

Browse files
committed
solve(0929): implement solution using hash map
1 parent b033d64 commit 97bd66f

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use std::collections::HashSet;
2+
3+
use crate::solutions::Solution;
4+
5+
impl Solution {
6+
pub fn num_unique_emails(emails: Vec<String>) -> i32 {
7+
let mut hash_set = HashSet::new();
8+
9+
for email in emails {
10+
let (local, domain) = email.split_once("@").unwrap();
11+
12+
let local = {
13+
let local = match local.split_once("+") {
14+
Some((local, _)) => local.to_owned(),
15+
None => local.to_owned(),
16+
};
17+
18+
local.replace(".", "")
19+
};
20+
21+
hash_set.insert(local.to_owned() + "@" + domain);
22+
}
23+
24+
hash_set.len() as i32
25+
}
26+
}
27+
28+
#[test]
29+
fn test() {
30+
assert_eq!(
31+
Solution::num_unique_emails(vec![
32+
"[email protected]".to_owned(),
33+
"[email protected]".to_owned(),
34+
"[email protected]".to_owned(),
35+
]),
36+
2
37+
);
38+
assert_eq!(
39+
Solution::num_unique_emails(vec![
40+
"[email protected]".to_owned(),
41+
"[email protected]".to_owned(),
42+
"[email protected]".to_owned()
43+
]),
44+
3
45+
);
46+
assert_eq!(
47+
Solution::num_unique_emails(vec![
48+
"[email protected]".to_owned(),
49+
"[email protected]".to_owned(),
50+
]),
51+
2
52+
);
53+
}

src/solutions/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod _0009_palindrome_number;
33
pub mod _0020_valid_parentheses;
44
pub mod _0026_remove_duplicates_from_sorted_array;
55
pub mod _0027_remove_element;
6+
pub mod _0929_unique_email_addresses;
67
pub mod _1848_minimum_distance_to_the_target_element;
78
pub mod _2351_first_letter_to_appear_twice;
89
pub mod _3046_split_the_array;

0 commit comments

Comments
 (0)