Unique Email Addresses - Problem

Ever wonder how Gmail handles email forwarding rules? This problem simulates a simplified version of that system!

You're given an array of email addresses, and you need to determine how many unique destinations they actually represent after applying these forwarding rules:

  • Dot Rule: In the local part (before @), all dots '.' are ignored
    Example: alice.bob@gmail.com โ†’ alicebob@gmail.com
  • Plus Rule: In the local part, everything after the first '+' is ignored
    Example: alice+shopping@gmail.com โ†’ alice@gmail.com

Important: These rules only apply to the local part (before @), not the domain!

Goal: Return the number of unique email addresses that will actually receive emails.

Input & Output

example_1.py โ€” Basic Example
$ Input: ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
โ€บ Output: 2
๐Ÿ’ก Note: First email: 'test.email+alex@leetcode.com' becomes 'testemail@leetcode.com' (remove dots and +alex). Second email: 'test.e.mail+bob.cathy@leetcode.com' becomes 'testemail@leetcode.com' (remove dots and +bob.cathy) - same as first! Third email: 'testemail+david@lee.tcode.com' becomes 'testemail@lee.tcode.com' (remove +david) - different domain. Result: 2 unique addresses.
example_2.py โ€” Single Email
$ Input: ["a@leetcode.com"]
โ€บ Output: 1
๐Ÿ’ก Note: Only one email provided, and it has no dots or plus signs, so it remains 'a@leetcode.com'. Result: 1 unique address.
example_3.py โ€” Multiple Plus Signs
$ Input: ["test+multiple+plus@example.com","test@example.com"]
โ€บ Output: 1
๐Ÿ’ก Note: First email: 'test+multiple+plus@example.com' becomes 'test@example.com' (only remove everything after the FIRST plus). Second email: 'test@example.com' is already normalized. Both emails resolve to the same address, so result is 1.

Visualization

Tap to expand
Email Normalization Pipelinetest.email+tag@example.comSplit at @test.email+tag@example.comRemove +tagtest.emailRemove dotstestemailCombinetestemail@example.comAdd to Settest.e.mail@example.comAlso normalizes to testemail@example.comHash Settestemail@example.comdifferent@example.comResult: 2 unique
Understanding the Visualization
1
Split at @ symbol
Separate each email into local part (before @) and domain part (after @)
2
Apply plus rule
Remove everything after the first '+' in the local part
3
Apply dot rule
Remove all '.' characters from the local part
4
Reconstruct & store
Combine normalized local part with domain and add to hash set
5
Count unique
The size of the hash set gives us the number of unique addresses
Key Takeaway
๐ŸŽฏ Key Insight: Use a hash set to automatically handle uniqueness - normalize once and let the data structure do the deduplication work for you!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

We process each email exactly once, and hash set operations are O(1) on average

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

In worst case, all emails are unique so we store n normalized emails in the hash set

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค emails.length โ‰ค 100
  • 1 โ‰ค emails[i].length โ‰ค 100
  • emails[i] consists 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
Asked in
Google 47 Meta 23 Amazon 18 Microsoft 12
42.3K Views
Medium Frequency
~15 min Avg. Time
1.8K 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