Unique Email Addresses
Problem
Each email has a local name and a domain name separated by an @. In the local name, dots (.) are ignored and everything from the first plus sign (+) onward is dropped; the domain is left untouched. Given a list of emails, return how many distinct addresses actually receive mail.
["[email protected]", "[email protected]", "[email protected]"]2def num_unique_emails(emails):
seen = set()
for email in emails:
local, domain = email.split("@")
local = local.split("+")[0].replace(".", "")
seen.add(local + "@" + domain)
return len(seen)
function numUniqueEmails(emails) {
const seen = new Set();
for (const email of emails) {
const [localRaw, domain] = email.split("@");
const local = localRaw.split("+")[0].replace(/\./g, "");
seen.add(local + "@" + domain);
}
return seen.size;
}
class Solution {
public int numUniqueEmails(String[] emails) {
Set<String> seen = new HashSet<>();
for (String email : emails) {
String[] parts = email.split("@");
String local = parts[0].split("\\+")[0].replace(".", "");
seen.add(local + "@" + parts[1]);
}
return seen.size();
}
}
int numUniqueEmails(vector<string>& emails) {
unordered_set<string> seen;
for (auto& email : emails) {
int at = email.find('@');
string local, domain = email.substr(at + 1);
for (int i = 0; i < at; i++) {
if (email[i] == '+') break;
if (email[i] != '.') local += email[i];
}
seen.insert(local + "@" + domain);
}
return (int)seen.size();
}
Explanation
Two emails that look different can still land in the same inbox. The plan is to convert every email into its true delivery form (a canonical address), then count how many distinct ones we get using a set.
For each email we split once on @ into local and domain. The domain is left alone. The local part gets two clean-ups: everything from the first + onward is dropped with split("+")[0], and all dots are removed with replace(".", "").
We rebuild the address as local + "@" + domain and drop it into the set seen. A set ignores duplicates automatically, so addresses that normalize to the same thing collapse into one entry. The answer is just len(seen).
Example: [email protected] and [email protected] both clean up to [email protected], while [email protected] becomes [email protected]. That is two distinct inboxes, so the answer is 2.
Each email is processed once with simple string operations, so the work is linear in the total number of characters.