↓Skip to main content
  1. LeetCode/

LeetCode 929: Unique Email Addresses

·2 mins· ·
LeetCode Easy Array Hash-Table String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-09-26

  • Submissions: Runtime Error → Wrong Answer ×3 → Accepted ×2

Approach
#

Normalize each address into a set (Hash Table Template 5): split at @ into local and domain, cut the local part at the first +, then remove every .; leave the domain alone. Put the @ back when rejoining, or different addresses can produce the same string.

My Attempts
#

  • Attempt 1 misspelled replace (AttributeError).
  • The next two rejoined with "".join(email), dropping the @: ["a@bx.com", "ab@x.com"] both became "abx.com", so the answer 2 came out as 1.
  • The one after wrote emails[0] + '@' + emails[1], mixing up email and emails, and raised IndexError with a single address.

Solution
#

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)

Complexity
#

Time O(total characters), space O(total characters).

Mistakes to Avoid
#

  • The rules apply only to the local part; keep the domain’s dots.
  • Keep the @ when rejoining: "a@bx.com" and "ab@x.com" are different addresses.

Interview Explanation
#

I split each address at @, cut the local part at + and drop its dots, keep the domain, rejoin, and count distinct results with a set. Time is linear in the total length.

Related

LeetCode 953: Verifying an Alien Dictionary
·2 mins
LeetCode Easy Array Hash-Table String
LeetCode 953 note: map letters to their alien index and compare each adjacent pair at the first differing character; with no difference, the shorter must come first
LeetCode 819: Most Common Word
·2 mins
LeetCode Easy Array Hash-Table String Counting
LeetCode 819 note: replace punctuation with spaces, lowercase, split, count, and pick the most frequent word not banned
LeetCode 748: Shortest Completing Word
·2 mins
LeetCode Easy Array Hash-Table String
LeetCode 748 note: count the plate’s letters in lowercase, then find the shortest word covering every count, taking the earliest on ties
LeetCode 599: Minimum Index Sum of Two Lists
·2 mins
LeetCode Easy Array Hash-Table String
LeetCode 599 note: map list1’s strings to indices, sum both indices while scanning list2, and keep every string with the minimum sum
LeetCode 804: Unique Morse Code Words
·1 min
LeetCode Easy Array Hash-Table String
LeetCode 804 note: translate each word into a Morse string and count the distinct results with a set
LeetCode 500: Keyboard Row
·1 min
LeetCode Easy Array Hash-Table String
LeetCode 500 note: make a set for each keyboard row and keep words whose lowercase character set is a subset of one row