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.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.
C++
Java
JavaScript
C
C#
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.
C++
Java
JavaScript
C
C#
Time Complexity: O(N * M), since per email processed once fully.
Space Complexity: O(N), enabling efficient tracking of unique addresses.
| 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. |
Unique Email Addresses - Two Solutions - Leetcode 929 Python • NeetCode • 23,456 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