929. Unique Email Addresses
easyCount the number of distinct delivery addresses after normalizing local-name rules: ignore everything after a plus sign and strip every dot. A clean parse + set count question.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Every valid address consists of a local name and a domain name, separated by the '@' sign. Besides lowercase letters, these names may contain '.'s or '+'s. If you add periods between some characters in the local name, mail sent there will be forwarded to the same address without dots in the local name. If you add a plus sign in the local name, everything after the first plus sign will be ignored. These rules apply only to the local name and not to the domain name. Given an array of strings emails where we send one message to each emails[i], return the number of different addresses that actually receive mail.
Constraints
1 <= emails.length <= 1001 <= emails[i].length <= 100emails[i] consists of lowercase English letters, '+', '.', and '@'.Each emails[i] contains exactly one '@' character.All local and domain names are non-empty.Local names do not start with a '+' character.Domain names end with the '.com' suffix.
Examples
Example 1
emails = ["[email protected]","[email protected]","[email protected]"]2Example 2
emails = ["[email protected]","[email protected]","[email protected]"]3Example 3
emails = ["[email protected]","[email protected]"]1Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Hints
Progressive — try the first before opening the next.
Hint 1
Split on '@' into local and domain parts.
Hint 2
On the local part, drop everything from the first '+' onward, then remove every '.'.
Hint 3
Re-join with '@' and insert the result into a hash set; the answer is the set's size.
Solution approach
Reveal approach
Hash set of canonical forms. Iterate each entry. Split it on the '@' character into local and domain. On the local part: if a '+' is present, slice off everything from that index onward; then strip all '.' characters. Concatenate the cleaned local with '@' and the domain and add to a set. After the loop, return the set's cardinality. O(L) time where L is the total character count across the input. O(L) space for the set.
Complexity
- Time
- O(L)
- Space
- O(L)
Related patterns
- hash-set
- string
Related problems
- 811. Subdomain Visit Count
- 819. Most Common Word
- 49. Group Anagrams
Asked at
Companies reported asking this problem (sourced from public Glassdoor, Blind, and Levels.fyi interview posts).
- Amazon
Practice these live with InterviewChamp.AI
Drill Unique Email Addresses and Hash Tables problems under real interview conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →