NeetCode #43LC-929EasyArrays & Hashing
← Back to All Problems#43 · #929 · Unique Email Addresses(独特的电子邮件地址)
📌 Problem Statement & Constraints
Every valid email address consists of a local name, an
@, and a domain name. In the local name, periods are ignored and everything from a + onward is ignored. Given a list of emails, return the number of distinct addresses that actually receive mail. Constraints: 1 <= emails.length <= 100.💡 Core Algorithmic Approaches
- Split each address at the
@to separate the local name from the domain. - Normalise the local name: cut it at the first
+, then remove all.characters. - Reassemble as
normalised_local + '@' + domainand add it to a set. - The answer is the size of the set. The
+truncation must happen before the dot removal (or vice versa) -- either order works, but the+cut must come first if you split on+, since the domain never contains a+.
💻 Benchmark Python3 Implementation
class Solution:
def numUniqueEmails(self, emails: List[str]) -> int:
seen = set()
for e in emails:
local, domain = e.split("@")
local = local.split("+")[0] # drop everything after '+'
local = local.replace(".", "") # dots are ignored
seen.add(local + "@" + domain)
return len(seen)⚡ Complexity Deep Dive
⏱️ Time Complexity
O(total characters): each email is normalised in one pass.
💾 Space Complexity
O(n) for the set of distinct addresses.
⚠️ Interview Pitfalls & Follow-ups
- Applying the dot/plus rules to the domain: they apply only to the local name;
[email protected]and[email protected]are the same mailbox, but[email protected]is not the same as[email protected]. - Splitting on
+without limiting the split:local.split('+')[0]is correct;local.split('+')and taking a fixed index is not. - Using
replace('.', '')on the whole email: this would also strip dots from the domain. - Forgetting that emails are guaranteed valid: no need for format validation.