Unique Email Addresses
Easy
Every email consists of a local name and a domain name, separated by the @ sign.
For example, inalice@leetcode.com
, alice
is the local name, andleetcode.com
is the domain name.
Besides lowercase letters, these emails may contain'.'
s or'+'
s.
If you add periods ('.'
) between some characters in thelocal namepart of an email address, mail sent there will be forwarded to the same address without dots in the local name. For example,"alice.z@leetcode.com"
and"alicez@leetcode.com"
forward to the same email address. (Note that this rule does not apply for domain names.)
If you add a plus ('+'
) in thelocal name, everything after the first plus sign will be ignored. This allows certain emails to be filtered, for example m.y+name@email.com
will be forwarded to my@email.com
. (Again, this rule does not apply for domain names.)
It is possible to use both of these rules at the same time.
Given a list ofemails
, we send one email to each address in the list. How many different addresses actually receive mails?
Example 1:
Note:
1 <= emails[i].length <= 100
1 <= emails.length <= 100
Each
emails[i]
contains exactly one'@'
character.
Analysis
基本思想就是将email都转化为统一的形式canonical form,即去掉'.', '+等字符的影响,最后返回unique元素hashset的大小即可。
Solution
LeetCode official - Using indexOf(), replaceAll() - (39 ms, faster than 34.09%)
Character by character, StringBuilder - (25 ms, faster than 72.51%)
(Preferred*) Another Character by character implementation by @FLAGbigoffer- (17ms, 89.80%)
We can do
BETTER
without usingreplace()
. Actually in some situations, we even don't need scan the entire email address. Consider this case:
a+nadgkj.ansjfnakjn.gaskjgb.ubeijbg.kjncna.oskenijb.golkn.asignoaeroib.gjoibv.oanod.safa.e.f.asdf.sa.df.a@g.com
. What we do here is to scan the email address until meeting the first
'+'
sign. Then scan reversely to find the'@'
sign.
Reference
https://leetcode.com/problems/unique-email-addresses/solution/
https://leetcode.com/problems/unique-email-addresses/discuss/186798/Java-7-liner-with-comment.
Last updated