algoadvance

Given a list of email addresses, we need to determine the number of unique email addresses.

Rules for processing each email address:

  1. Local name:
    • The part before the ‘@’ character is called the local name.
    • In the local name, a period . has no effect (i.e., “alice.z@example.com” is the same as “alicez@example.com”).
    • Plus + sign and its following characters in the local name should be ignored (i.e., “alice+foo@example.com” is the same as “alice@example.com”).
  2. Domain name:
    • The part after the ‘@’ character is called the domain name and it remains as is.

Example:

The “test.email+alex@leetcode.com” and “test.e.mail+bob.cathy@leetcode.com” reduce to “testemail@leetcode.com”, while “testemail+david@lee.tcode.com” reduces to “testemail@lee.tcode.com”.

Function Signature:

def numUniqueEmails(emails: List[str]) -> int:

Clarifying Questions

  1. Can we consider the list of email addresses to be always valid and non-empty?
  2. Is there an upper limit on the number of email addresses we might receive in the list?

Strategy

  1. Iterate through each email in the input list.
  2. Split each email into the local name and the domain name using the ‘@’ character.
  3. Process the local name:
    • Remove substring starting from + (if exists) to the end.
    • Remove all . characters.
  4. Combine processed local name with the domain name.
  5. Use a set to store unique processed email addresses.
  6. Return the size of the set.

Time Complexity

The time complexity for the solution is O(n * m) where:

This is due to processing each email address and the operations applied to split and modify the strings.

Code

from typing import List

def numUniqueEmails(emails: List[str]) -> int:
    unique_emails = set()
    
    for email in emails:
        local, domain = email.split('@')
        local = local.split('+')[0]  # Ignore the characters after '+'
        local = local.replace('.', '')  # Remove all periods
        unique_email = local + '@' + domain
        unique_emails.add(unique_email)
    
    return len(unique_emails)

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

Feel free to test the function with different input cases to ensure it handles all edge cases correctly.

Try our interview co-pilot at AlgoAdvance.com