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
def num_unique_emails(emails):
seen = set()
for email in emails:
local, domain = email.split('@')
local = local.split('+')[0]
local = local.replace('.', '')
seen.add(local + '@' + domain)
return len(seen)
# Example usage
emails = ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
print(num_unique_emails(emails)) # Output: 2
emails = ["a@leetcode.com","b@leetcode.com","c@leetcode.com"]
print(num_unique_emails(emails)) # Output: 3
Initialization:
seen = set()
: A set called seen
is initialized to store unique email addresses after processing. Sets are used to automatically handle duplicates.Iterating through Emails:
for email in emails:
: The code iterates through each email address in the input list emails
.Splitting Local and Domain Names:
local, domain = email.split('@')
: Each email is split into two parts at the @
symbol. The part before @
is considered the local
name, and the part after @
is the domain
name.Processing the Local Name:
local = local.split('+')[0]
: If there is a +
in the local name, the part after the first +
is discarded. This is done by splitting the local name at the +
and taking only the first part.local = local.replace('.', '')
: All .
characters in the local name are removed.Creating the Normalized Email Address:
seen.add(local + '@' + domain)
: The processed local name and the original domain name are concatenated with @
in between, and the resulting string is added to the seen
set. Because it's a set, duplicate emails will not be added.Returning the Count of Unique Emails:
return len(seen)
: After processing all emails, the code returns the number of elements in the seen
set, which represents the number of unique email addresses.split
and replace
on each email, which take O(M) time where M is the length of the email string.seen
set. Each email can take up to O(M) space, where M is the length of the email.