⚡Flashcard Drill➔
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

  1. Split each address at the @ to separate the local name from the domain.
  2. Normalise the local name: cut it at the first +, then remove all . characters.
  3. Reassemble as normalised_local + '@' + domain and add it to a set.
  4. 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.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔

🧩 Related Arrays & Hashing Problems

中文速览与面试要点

独特的电子邮件地址

每个有效电子邮件地址都由一个本地名和一个域名组成,以 @ 符号分隔。在本地名部分中,点号 . 会被忽略;加号 + 后面的所有内容都会被忽略。给定一个字符串数组 emails,返回实际收到邮件的 不同 电子邮件地址的数量。约束:1 ≤ emails.length ≤ 100;每个 emails[i] 长度在 1 到 100 之间;均为合法邮箱格式。

核心要点

  • 规范化每个邮箱(标准解) :按 @ 拆成 local 与 domain;local 先按 + 截断,再删除所有 .;最后拼回 local@domain 。
  • 为什么用集合去重 :不同原始写法可能规范化成同一地址,必须用集合统计「不同」地址数。
  • 处理顺序 :先截断 + 再删点,或先删点再截断,结果相同(因为 + 之后的内容整体丢弃,与点无关)。
  • 边界 :若本地名不含 +, split("+")[0] 返回整个本地名,天然正确。

常见陷阱

  • 只对本地名去点却忘记截断 + :或反之,两个规则必须同时应用。
  • 对域名也做去点或截断 :规则 只作用于本地名 ,域名必须原样保留(否则 [email protected] 与 a@bc 会被错误合并)。
  • 忘记用集合去重 :不同原始写法可能规范化成同一地址,用列表计数会重复统计。
  • 用 replace(".", "") 作用于整个邮箱 :会把域名中的点也删掉,导致错误合并。

参考实现

class Solution:
    def numUniqueEmails(self, emails: list[str]) -> int:
        seen = set()
        for e in emails:
            local, domain = e.split('@')
            local = local.split('+')[0].replace('.', '')   # 截断 + 后内容,再去点号
            seen.add(local + '@' + domain)
        return len(seen)