Back to solutions

Unique Email Addresses

EasyArrays

Count distinct delivery addresses after normalising email local parts.

Constraints
  • 1 <= n <= 100
  • Each email has exactly one '@'

Normalise then deduplicate

Time O(total characters)Space O(n)

For each email split at '@'. In the local part, drop everything from the first '+' and remove all dots; leave the domain untouched. Rejoin and insert into a set. The set size is the number of distinct delivery addresses.

Key terms
local part:
The portion of the email before the '@'.
normalisation:
Rewriting each address into a canonical form so equivalents collide.
string normalize(const string& email) {
    size_t at = email.find('@');
    string local = email.substr(0, at), domain = email.substr(at), clean;
    for (char c : local) {
        if (c == '+') break;
        if (c != '.') clean += c;
    }
    return clean + domain;
}
Step by step
  1. Split the email into local and domain at '@'.
  2. In local, stop at the first '+' and delete every '.'.
  3. Concatenate the cleaned local with the original domain.
  4. Insert into a set; the final answer is the set's size.