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.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a stack of mail addressed to the exact same people, but written in many messy ways. The brute force approach is like taking every single email address on your list, cleaning them up one by one, and keeping a separate pile for the unique ones.
Here's how the algorithm would work step-by-step:
def numUniqueEmails(emails):
unique_collection_pile = []
# We process each email individually to simulate cleaning the messy mail pile
for current_email in emails:
local_name, domain_name = current_email.split('@')
if '+' in local_name:
local_name = local_name.split('+')[0]
# Dots are ignored in the local name, so we remove them completely
local_name = local_name.replace('.', '')
official_email_version = local_name + '@' + domain_name
# We check uniqueness by comparing against our pile before adding it
if official_email_version not in unique_collection_pile:
unique_collection_pile.append(official_email_version)
return len(unique_collection_pile)To find out how many unique email addresses actually receive mail, we need to clean up each address by applying specific formatting rules. By simplifying every email into its true destination form and collecting them in a single place, we can easily count how many distinct destinations exist.
Here's how the algorithm would work step-by-step:
def num_unique_emails(email_address_list):
unique_destination_set = set()
# Using a set ensures duplicate destinations are automatically discarded.
for raw_email_address in email_address_list:
local_name, domain_name = raw_email_address.split('@')
if '+' in local_name:
local_name = local_name.split('+')[0]
# Dots are ignored in local names, so they must be removed.
local_name = local_name.replace('.', '')
actual_destination = local_name + '@' + domain_name
unique_destination_set.add(actual_destination)
return len(unique_destination_set)| Case | How to Handle |
|---|---|
| Empty or null input array of emails | Return zero immediately since there are no email addresses to process. |
| Emails with multiple plus signs in the local name | Ignore all characters starting from the very first plus sign encountered. |
| Emails containing no local name or an empty domain | Assume well-formed constraints as per standard problem guarantees or skip invalid formats. |
| Local names consisting entirely of periods | Strip all periods resulting in an empty local name that still correctly concatenates with the domain. |
| Large input arrays with millions of identical emails | Use a hash set to store processed unique addresses, ensuring efficient O(N) time complexity and deduplication. |
| Domain names with mixed or uppercase letters | Preserve the exact casing of the domain name since domains are case-sensitive. |
| Local names with mixed case letters | Preserve the casing of the local name because punctuation rules only apply to periods and plus signs, not letters. |