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
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
โ Linear Growth
Space Complexity
O(n)
In worst case, all emails are unique so we store n normalized emails in the hash set
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code