Skip to content

Commit 02c20ee

Browse files
committed
Add num_unique_emails
1 parent 9fc3ce9 commit 02c20ee

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ LeetCode is a website that has programming-related questions that are designed t
3232
| 713 | Subarray Product Less Than K | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/num_subarray_product_less_than_k.rs) | [Leetcode](https://leetcode.com/problems/subarray-product-less-than-k/) | Medium |
3333
| 774 | Minimize Max Distance to Gas Station | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/minmax_gas_dist.rs) | [Leetcode](https://leetcode.com/problems/minimize-max-distance-to-gas-station/) | Medium |
3434
| 875 | Koko Eating Bananas | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/min_eating_speed.rs) | [Leetcode](https://leetcode.com/problems/koko-eating-bananas/) | Medium |
35+
| 929 | Unique Email Addresses | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/num_unique_emails.rs) | [Leetcode](https://leetcode.com/problems/unique-email-addresses/) | Easy |
3536
| 1035 | Uncrossed Lines | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/max_uncrossed_lines.rs) | [Leetcode](https://leetcode.com/problems/uncrossed-lines/) | Medium |
3637
| 1134 | Armstrong Number | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/problem/is_good_array.rs) | [Leetcode](https://leetcode.com/problems/armstrong-number/) | Easy |
3738
| 1137 | N-th Tribonacci Number | [Rust](https://github.com/martinxxd/leetcode-rust/tree/master/./src/leetcode/challenge/tribonacci.rs) | [Leetcode](https://leetcode.com/problems/n-th-tribonacci-number/) | Easy |

src/leetcode/challenge/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod max_length;
55
mod minmax_gas_dist;
66
mod moves_to_chessboard;
77
mod num_distinct;
8+
mod num_unique_emails;
89
mod shortest_distance;
910
mod shortest_path;
1011
mod spiral_order;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use std::collections::HashSet;
2+
3+
// 929. Unique Email Addresses, Easy
4+
// https://leetcode.com/problems/unique-email-addresses/
5+
impl Solution {
6+
pub fn num_unique_emails(emails: Vec<String>) -> i32 {
7+
let mut set = HashSet::new();
8+
9+
for email in emails {
10+
let email: Vec<_> = email.split('@').collect();
11+
let mut vhost: String = email[0].to_string().replace(".", "");
12+
if vhost.contains("+") {
13+
vhost = vhost.split('+').next().unwrap().to_string();
14+
}
15+
16+
let domain: String = email[1].to_string();
17+
18+
set.insert(format!("{}@{}", vhost, domain));
19+
}
20+
21+
return set.len() as i32;
22+
}
23+
}
24+
25+
struct Solution {}
26+
27+
#[cfg(test)]
28+
mod tests {
29+
use super::*;
30+
use crate::vec_string;
31+
32+
#[test]
33+
fn test_num_unique_emails() {
34+
assert_eq!(
35+
Solution::num_unique_emails(vec_string![
36+
"test.email+alex@leetcode.com",
37+
"test.e.mail+bob.cathy@leetcode.com",
38+
"testemail+david@lee.tcode.com"
39+
]),
40+
2
41+
);
42+
}
43+
44+
#[test]
45+
fn test_num_unique_emails2() {
46+
assert_eq!(Solution::num_unique_emails(vec_string!["a@leetcode.com", "b@leetcode.com", "c@leetcode.com"]), 3);
47+
}
48+
49+
#[test]
50+
fn test_num_unique_emails3() {
51+
assert_eq!(Solution::num_unique_emails(vec_string!["a@leetcode.com", "a@leetcode.com"]), 1);
52+
}
53+
54+
#[test]
55+
fn test_num_unique_emails4() {
56+
assert_eq!(Solution::num_unique_emails(vec_string![]), 0);
57+
}
58+
}

0 commit comments

Comments
 (0)