Given a list of email addresses, we need to determine the number of unique email addresses.
Rules for processing each email address:
.
has no effect (i.e., “alice.z@example.com” is the same as “alicez@example.com”).+
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”).Example:
["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
2
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:
+
(if exists) to the end..
characters.The time complexity for the solution is O(n * m) where:
n
is the number of email addresses.m
is the average length of an email address.This is due to processing each email address and the operations applied to split and modify the strings.
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.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?