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
Unique Email Addresses INPUT emails[] array test.email+tag @leetcode.com [0] test.e.mail @leetcode.com [1] test.email.leet+tag @leetcode.com [2] Rules Legend '.' in local: ignored '+' in local: truncate after '@': separates local/domain Domain rules NOT affected ALGORITHM STEPS 1 Split at '@' Separate local + domain 2 Remove after '+' Truncate local name 3 Remove all '.' From local name only 4 Add to HashSet Track unique emails HashSet Contents [email protected] [email protected] DUP [email protected] Set size: 2 unique FINAL RESULT Output 2 OK - Verified Breakdown: [email protected] --> from emails[0], [1] [email protected] --> from emails[2] 3 emails input 2 unique addresses Key Insight: Using a HashSet automatically handles duplicate detection. Transform each email by: 1) Splitting at '@' 2) Removing everything after '+' in local 3) Removing all '.' from local Time: O(n*m) where n=emails, m=avg length | Space: O(n*m) for HashSet storage TutorialsPoint - Unique Email Addresses | Hash Set for Unique Tracking
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