Unique Email Addresses

To solve this problem, we need to understand the rules for creating email addresses. The ‘+’ symbol will ignore everything after it in the local part of the email address, and the ‘.’ symbol will be ignored. Given that, we need to:

  • Split the email by the ‘@’ symbol to separate the local and domain names.
  • Within the local name, find the ‘+’ symbol and discard everything after it.
  • Remove all ‘.’ symbols from the local name.
  • Join the processed local name with the domain name.
  • Add the result to a set (which automatically removes duplicates).
  • Return the size of the set, which represents the number of unique email addresses.

Here is the Python solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution:
    def numUniqueEmails(self, emails: List[str]) -> int:
        unique_emails = set()

        for email in emails:
            local_name, domain_name = email.split('@')
            if '+' in local_name:
                local_name = local_name[:local_name.index('+')]
            local_name = local_name.replace('.', '')
            unique_emails.add(local_name + '@' + domain_name)

        return len(unique_emails)

This code will go through each email in the provided list, process it according to the rules given in the problem statement, and add it to the set of unique emails. Because a set only stores unique items, any duplicate processed emails will not be added. Finally, the function returns the number of unique emails by returning the size of the set.