Domain 1EasyCORE

Unique Email Addresses

The problem requires us to determine the number of unique email addresses that actually receive emails after applying certain rules to the local name part of the email address. The inputs are an array of strings where each string represents an email address. The outputs should be the count of distinct email addresses after processing.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#String

929. Unique Email Addresses

Model: qwen2.5-coder:14b
Topics: String
FAANG asked: 1
Acceptance rate: 67.2%


Problem Breakdown

The problem requires us to determine the number of unique email addresses that actually receive emails after applying certain rules to the local name part of the email address. The inputs are an array of strings where each string represents an email address. The outputs should be the count of distinct email addresses after processing.

Inputs:

  • An array of strings emails where each string is a valid email address.

Outputs:

  • An integer representing the number of unique email addresses that receive emails.

Implicit Constraints or Tricky Edge Cases:

  • The local name part may contain periods '.' which should be ignored.
  • The local name part may contain a plus '+' sign, where everything after the first plus is ignored.
  • Domain names are not affected by these rules.
  • Each email contains exactly one '@' character.

Intuition

A brute-force approach would involve iterating through each email, applying the rules to normalize it, and then storing all normalized emails in a set to count unique addresses. However, this approach is straightforward but may not be optimal in terms of time complexity if the input size is large.

The key insight is that we can simplify the problem by normalizing each email address according to the given rules before adding it to a set. This way, we ensure that all variations of an email address are treated as identical, and only unique normalized addresses are counted.

Approach

  1. Initialize a Set: Use a set to store unique email addresses after normalization.
  2. Iterate Through Emails: For each email in the input list:
    • Split the email into local name and domain name using the '@' character.
    • Process the local name by removing all periods '.' and ignoring everything after the first plus '+'.
    • Concatenate the processed local name with the domain name to form the normalized email address.
  3. Add to Set: Add the normalized email address to the set.
  4. Return Result: The size of the set represents the number of unique email addresses.

Complexity Analysis

  • Time Complexity: O(n * m), where n is the number of emails and m is the average length of an email. This is because we iterate through each character of each email to normalize it.
  • Space Complexity: O(n * m) for storing the normalized email addresses in the set.

Solution

def numUniqueEmails(emails):
    unique_emails = set()
    
    for email in emails:
        local, domain = email.split('@')
        # Remove all periods from the local name
        local = local.replace('.', '')
        # Ignore everything after the first plus sign
        if '+' in local:
            local = local[:local.index('+')]
        
        normalized_email = local + '@' + domain
        unique_emails.add(normalized_email)
    
    return len(unique_emails)

# Example usage:
emails1 = ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
print(numUniqueEmails(emails1))  # Output: 2

emails2 = ["a@leetcode.com","b@leetcode.com","c@leetcode.com"]
print(numUniqueEmails(emails2))  # Output: 3

Edge Cases Handled

  • Empty Input: The function will return 0 if the input list is empty.
  • No '+' or '.': If the local name contains neither '+' nor '.', it remains unchanged.
  • Multiple '+': Only the first occurrence of '+' and everything after it are ignored.
  • Multiple '.': All periods in the local name are removed.