Taro Logo

Unique Email Addresses

Easy
1 views
2 months ago

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 '+'.

  • For example, in "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.

  • For example, "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.

  • For example, "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
Sample Answer
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

Explanation:

  1. Initialization:

    • seen = set(): A set called seen is initialized to store unique email addresses after processing. Sets are used to automatically handle duplicates.
  2. Iterating through Emails:

    • for email in emails:: The code iterates through each email address in the input list emails.
  3. 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.
  4. 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.
  5. 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.
  6. 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.

Big O Time Complexity

  • O(N * M), where N is the number of emails and M is the maximum length of an email. This is because we iterate through each email (O(N)) and perform string operations like split and replace on each email, which take O(M) time where M is the length of the email string.

Big O Space Complexity

  • O(N * M), where N is the number of emails and M is the average length of the processed email. This is because, in the worst-case scenario, all processed emails are unique and will be stored in the seen set. Each email can take up to O(M) space, where M is the length of the email.