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 "alice@leetcode.com", "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, "alice.z@leetcode.com" and "alicez@leetcode.com" 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, "m.y+name@email.com" will be forwarded to "my@email.com".

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
$ Input: emails = ["test.email+tag@leetcode.com","test.e.mail@leetcode.com","test.email.leet+tag@leetcode.com"]
Output: 2
💡 Note: After normalization: "test.email+tag@leetcode.com" becomes "testemail@leetcode.com", "test.e.mail@leetcode.com" becomes "testemail@leetcode.com" (same as first), and "test.email.leet+tag@leetcode.com" becomes "testemaillet@leetcode.com". So 2 unique addresses.
Example 2 — Only Dot Normalization
$ Input: emails = ["a@leetcode.com","b@leetcode.com","c@leetcode.com"]
Output: 3
💡 Note: No dots or plus signs to normalize, so all three emails remain unique: "a@leetcode.com", "b@leetcode.com", "c@leetcode.com".
Example 3 — Plus Sign Filtering
$ Input: emails = ["alice+work@example.com","alice+personal@example.com","alice@example.com"]
Output: 1
💡 Note: All emails normalize to "alice@example.com" 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 testemail@leetcode.com testemail@leetcode.com DUP testemailleet@leetcode.com Set size: 2 unique FINAL RESULT Output 2 OK - Verified Breakdown: testemail@leetcode.com --> from emails[0], [1] testemailleet@leetcode.com --> 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