Link: Unique Email Addresses
Thought
Brutal Force Solution
This question is pretty simple (and it actually is an esay one). A naive approache is, split the name and domain.
For the name, remove every thing after first ‘+’, and then replace all ‘.’ as empty. For the domain, just keep it is.
Then, use a set to make sure every thing is unique. After processed all emails, count the length of the set.
Code
class Solution:
def numUniqueEmails(self, emails):
"""
:type emails: List[str]
:rtype: int
"""
hist = set()
cnt = 0
for email in emails:
name, domain = email.split('@')
name = name.split('+')[0]
name=name.replace('.', '')
if name + '@' + domain not in hist:
hist.add(name+'@'+domain)
cnt += 1
return cnt
Runtime
Time: O(n)
Space: O(n)