Watch 10 video solutions for Unique Email Addresses, a easy level problem involving Array, Hash Table, String. This walkthrough by NeetCode has 25,597 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |