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 '+'.
"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.
"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.
"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.
Example 1:
Input: emails = ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"] Output: 2 Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails.
Example 2:
Input: emails = ["a@leetcode.com","b@leetcode.com","c@leetcode.com"] Output: 3
Constraints:
1 <= emails.length <= 1001 <= emails[i].length <= 100emails[i] consist of lowercase English letters, '+', '.' and '@'.emails[i] contains exactly one '@' character.'+' character.".com" suffix.".com" suffix.Problem Overview: You receive a list of email addresses. Certain normalization rules apply to the local name: dots (.) are ignored and everything after a plus (+) is discarded before the @. The task is to apply these rules and count how many unique addresses actually receive mail.
Approach 1: Using Set to Handle Unique Emails (O(n * m) time, O(n * m) space)
Iterate through each email and normalize it before counting. Split the address into local and domain around the @. Inside the local part, remove everything after the first +, then delete all dots. Reconstruct the normalized address and insert it into a set. Because a set stores only unique elements, duplicate normalized emails are automatically ignored. The algorithm scans each character of every email once, giving O(n * m) time where n is the number of emails and m is the average length.
This approach relies heavily on fast membership checks from a hash table. Sets provide average O(1) insertion and lookup, making them ideal for uniqueness problems. The logic is straightforward and easy to implement in any language using basic string operations.
Approach 2: Email Transformation with String Array Manipulation (O(n * m) time, O(n * m) space)
This method explicitly transforms the email step by step using string arrays. First split the email using @ into two parts. Convert the local name into a character array or iterate through it with a pointer. Stop processing when a + is encountered and skip dots while building the cleaned local name. Finally combine the cleaned local name with the domain and insert the result into a set to track uniqueness.
The key difference is the manual transformation process. Instead of chained string operations, you iterate through characters and build the normalized string incrementally. This gives tighter control over processing and can slightly reduce intermediate allocations in some languages. The complexity remains O(n * m) because each character is processed at most once.
Both solutions treat the problem as normalization followed by deduplication. The normalization stage applies deterministic rules, and the set guarantees uniqueness. The problem primarily tests comfort with array iteration, string parsing, and hash-based collections.
Recommended for interviews: The set-based normalization approach is what interviewers expect. It demonstrates clean thinking: transform each email once, then rely on a hash set for uniqueness. Brute reasoning about duplicates shows understanding, but recognizing that normalization + hashing solves the problem efficiently shows stronger problem‑solving skill.
In this approach, we'll use a set to store unique email addresses. We'll iterate over each email, split it into local and domain parts, then manipulate the local part by removing any characters after the first '+' and all periods '.' before rejoining with the domain. Finally, we'll add the standardized email to the set. The size of the set gives the count of unique emails.
In Python, we use a set to hold unique email addresses. We split each email by '@' to get local and domain parts. The local part is then split at the '+' to ignore everything beyond it and replaced '.' with ''. The processed local part is then combined with the domain part before adding to the set. The number of unique emails is the size of the set.
Time Complexity: O(N * M) where N is the number of emails and M is the maximum length of an email address.
Space Complexity: O(N) due to the storage of unique emails in a set.
In this approach, instead of transforming emails directly using a set, we'll utilize arrays to manipulate the email parts. We'll maintain a boolean array for discovered unique emails and an index array to track up to where the email is unique. This efficient tracking helps us determine unique addresses without full storage, but more focused on transformations directly performed within the iteration loop.
This is similar to Approach 1, but a slight variation in explanation to describe transformations as stepwise and using string-based mutation directly. The Python simplicity helps accomplish similar efficiency with identical methods.
Time Complexity: O(N * M), since per email processed once fully.
Space Complexity: O(N), enabling efficient tracking of unique addresses.
We can use a hash table s to store all unique email addresses. Then, we traverse the array emails. For each email address, we split it into the local part and the domain part. We process the local part by removing all dots and ignoring characters after a plus sign. Finally, we concatenate the processed local part with the domain part and add it to the hash table s.
In the end, we return the size of the hash table s.
The time complexity is O(L), and the space complexity is O(L), where L is the total length of all email addresses.
Python
Java
C++
Go
TypeScript
Rust
JavaScript
| Approach | Complexity |
|---|---|
| Using Set to Handle Unique Emails | Time Complexity: O(N * M) where N is the number of emails and M is the maximum length of an email address. |
| Email Transformation with String Array Manipulation | Time Complexity: O(N * M), since per email processed once fully. |
| Hash Table | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Using Set to Handle Unique Emails | O(n * m) | O(n * m) | Best general solution when you want simple normalization and fast uniqueness checks |
| Email Transformation with String Array Manipulation | O(n * m) | O(n * m) | Useful when implementing manual parsing or minimizing repeated string operations |
Unique Email Addresses - Two Solutions - Leetcode 929 Python • NeetCode • 25,597 views views
Watch 9 more video solutions →Practice Unique Email Addresses with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor