Back to solutions
Unique Email Addresses
EasyArraysCount 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
- Split the email into local and domain at '@'.
- In local, stop at the first '+' and delete every '.'.
- Concatenate the cleaned local with the original domain.
- Insert into a set; the final answer is the set's size.