-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0929-unique-email-addresses.js
50 lines (45 loc) · 1.09 KB
/
0929-unique-email-addresses.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Built-in Functions Solution
* Hash Set - Unique Emails
* Time O(N * K) | Space O(N)
* https://leetcode.com/problems/unique-email-addresses
* @param {string[]} emails
* @return {number}
*/
var numUniqueEmails = function (emails) {
const valid = emails.map(email => {
const [local, domain] = email.split("@");
return (
local.split("+").shift().split(".").join("") + "@" + domain
);
});
return new Set(valid).size;
};
/**
* Manual Solution
* Hash Set - Unique Emails
* Time O(N * K) | Space O(N)
* https://leetcode.com/problems/unique-email-addresses
* @param {string[]} emails
* @return {number}
*/
var numUniqueEmails = function (emails) {
const uniqEmails = new Set();
for (let email of emails) {
let cleanEmail = "";
for (let i = 0; i < email.length; i++) {
if (email[i] === "@") {
cleanEmail += email.slice(i);
break;
} else if (email[i] === "+") {
while (email[i] !== "@") i++;
cleanEmail += email.slice(i);
break;
} else if (email[i] !== ".") {
cleanEmail += email[i];
}
}
uniqEmails.add(cleanEmail);
}
return uniqEmails.size;
};