Unique Email Addresses — LeetCode 929 Python Solution
EasyArrayHash TableString
- Problem
- #929
- Pattern
- Hash Map
- Reading time
- 3 min
- Source
- leetcode.com
The problem
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 '+'.
Example
- 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.
Python solution
Python
class Solution:
def numUniqueEmails(self, emails: List[str]) -> int:
s = set()
for email in emails:
local, domain = email.split("@")
t = []
for c in local:
if c == ".":
continue
if c == "+":
break
t.append(c)
s.add("".join(t) + "@" + domain)
return len(s)Complexity
| Measure | Complexity |
|---|---|
| Time | O(L) |
| Space | O(L), where L is the total length of all email addresses auxiliary |
Pattern: Hash Map
Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 929. Unique Email Addresses is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Hash Table.
The hash map guide has the Python template for the pattern and the 709 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 929. Unique Email Addresses?
- LeetCode 929. Unique Email Addresses is rated Easy on LeetCode.
- What is the time complexity of LeetCode 929. Unique Email Addresses?
- The Python solution on this page runs in O(L).
- What is the space complexity of LeetCode 929. Unique Email Addresses?
- The Python solution on this page uses O(L), where L is the total length of all email addresses auxiliary space.
- What topics does LeetCode 929. Unique Email Addresses cover?
- LeetCode 929. Unique Email Addresses is tagged Array, Hash Table and String on LeetCode.