Google Leetcode 929 Unique Email Addresses

Google Interview Questions - Leetcode 929 Unique Email Addresses Fast Solution
Level: Easy

Analysis

  • Every valid email has a local name and the domain name separated by @ symbol.
  • Dots in the local name are removed when checking for uniqueness.
  • any character after the + sign is ignored.
  • Domain names are left as they are to compare.

Example

The bellow are all the same mailbox.
testme@example.com
test.me@example.com
test.me+hey@example.com
test.me+jo@example.com
test.me+ee@example.com
But the bellow is a different domain.
testme@examplex.com

Solution in Python 3

This solution uses the Slice in Python to slice the string as needed and a List to store unique emails found. One major trick is to use email[i:] and not just email[i]

And the results output

Output
class Solution:
    def numUniqueEmails(self, emails: List[str]) -> int:
        seen = []
        for email in emails:
            i = email.index('@')
            local = email[0:i]
            rest = email[i:]
            if("+" in local):
                local = local[0: local.index('+')]
            local = local.replace(".","")
            localrest = local+rest
            if localrest not in seen: 
                seen.append(localrest)
        return len(seen)

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Exit mobile version