Search~15 min2s · 256 MBtarget O(n × L) time, O(n × L) spacehashing
A search app collects signups for an early-access list. People type their email address in different ways. Many mail services ignore capital letters, ignore dots in the part before the @, and ignore a tag after a plus sign. So Ann.Lee+news@Mail.com and annlee@mail.com reach the same inbox.
The product team wants one account for each person, using the first signup of that person.
Write unique_signups(emails).
local@domain, with exactly one @.@, cut everything from its first +, remove its dots, and make both parts lower case. Dots and capitals in the domain still matter only through lower casing.Input: emails = ["Ann.Lee@Mail.com", "ann.lee+news@mail.com", "bob@mail.com", "annlee@mail.com", "bob@mail.org"]
Output: ["Ann.Lee@Mail.com", "bob@mail.com", "bob@mail.org"]The table gives the normal form of each address and whether it is kept.
| Address | Normal form | Kept? |
|---|---|---|
| Ann.Lee@Mail.com | annlee@mail.com | yes |
| ann.lee+news@mail.com | annlee@mail.com | no |
| bob@mail.com | bob@mail.com | yes |
| annlee@mail.com | annlee@mail.com | no |
| bob@mail.org | bob@mail.org | yes |
The table shows each address with its normal form. The addresses ann.lee+news@mail.com and annlee@mail.com have the same normal form as the first one, so they are skipped. The address bob@mail.org has a different domain.
Input: emails = ["a@x.io"]
Output: ["a@x.io"]One address is kept.
Input: emails = ["x+1@y.io", "x+2@y.io", "x@y.io"]
Output: ["x+1@y.io"]All three addresses have the normal form x@y.io. The first one is kept, with its tag.
1 ≤ emails.length ≤ 100,000 (signups)@@, and a plus sign is never first in the local partComparing each address with every address kept so far costs about n × n / 2 comparisons, 5 × 10^9 at n = 100,000. Computing the normal form of each address once and keeping the normal forms in a set costs about one set lookup for each address.