Unique Email Addresses

easy string hash set

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.

Output2
The first two normalize to [email protected]; the third becomes [email protected].

def 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();
}
Time: O(L) Space: O(L)