Unique Email Addresses - Problem

Every valid email consists of a local name and a domain name, separated by the '@' sign. Besides lowercase letters, the email may contain one or more '.' or '+'.

For example, in "[email protected]", "alice" is the local name, and "leetcode.com" is the domain name.

If you add periods '.' between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name. Note that this rule does not apply to domain names.

For example, "[email protected]" and "[email protected]" forward to the same email address.

If you add a plus '+' in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered. Note that this rule does not apply to domain names.

For example, "[email protected]" will be forwarded to "[email protected]".

It is possible to use both of these rules at the same time. Given an array of strings emails where we send one email to each emails[i], return the number of different addresses that actually receive mails.

Input & Output

Example 1 — Multiple Normalization Rules
Output: 2
💡 Note: After normalization: "[email protected]" becomes "[email protected]", "[email protected]" becomes "[email protected]" (same as first), and "[email protected]" becomes "[email protected]". So 2 unique addresses.
Example 2 — Only Dot Normalization
Output: 3
💡 Note: No dots or plus signs to normalize, so all three emails remain unique: "[email protected]", "[email protected]", "[email protected]".
Example 3 — Plus Sign Filtering
Output: 1
💡 Note: All emails normalize to "[email protected]" because everything after the plus sign in the local part is ignored.

Constraints

  • 1 ≤ emails.length ≤ 100
  • 1 ≤ emails[i].length ≤ 100
  • emails[i] consist of lowercase English letters, '+', '.' and '@'
  • Each emails[i] contains exactly one '@' character
  • All local and domain names are non-empty
  • Local names do not start with a '+' character
  • Domain names end with the ".com" suffix

Visualization

Tap to expand
Email Normalization: Input → Process → Output[email protected][email protected][email protected]Input EmailsNormalization Rules1. Remove dots from local part2. Ignore text after + in local part[email protected][email protected][email protected]Normalized EmailsDuplicate DetectionFirst two normalize to same emailOutput: 2 unique email addresses
Understanding the Visualization
1
Input
Array of email addresses with dots and plus signs
2
Normalize
Remove dots from local part, ignore text after plus signs
3
Count
Count unique normalized email addresses
Key Takeaway
🎯 Key Insight: Use email normalization rules with a hash set to efficiently count unique addresses in O(n) time
Asked in
Google 45 Facebook 32 Apple 28 Microsoft 25
189.0K Views
High Frequency
~15 min Avg. Time
2.3K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen