leetcode929 Unique Email Addresses

Every email consists of a local name and a domain name, separated by the @ sign.

For example, in alice@leetcode.com, alice is the local name, and leetcode.com is the domain name.

Besides lowercase letters, these emails may contain '.'s or '+'s.

If you add periods ('.') between some characters in the local name part 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 the local 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 of emails, we send one email to each address in the list. How many different addresses actually receive mails?

每封电子邮件由一个本地名称和一个域名组成,以@符号分隔。

例如,在alice@leetcode.com中,alice是本地名,leetcode.com是域名。

除了小写字母,这些邮件还可能包含' '。”或“+”。

如果您在电子邮件地址的本地名称部分的某些字符之间添加句点('.'),那么发送到那里的邮件将被转发到相同的地址,而本地名称中没有点号。例如,“alice.z@leetcode.com”和“alicez@leetcode.com”转发到相同的电子邮件地址。(请注意,此规则不适用于域名。)

如果在本地名称中添加加号('+'),第一个加号后面的所有内容都将被忽略。这允许某些电子邮件被过滤,例如m.y+name@email.com将被转发到my@email.com。(同样,这条规则不适用于域名。)

同时使用这两个规则是可能的。

给定一个电子邮件列表,我们向列表中的每个地址发送一封电子邮件。有多少不同的地址实际接收邮件?

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
    public int numUniqueEmails(String[] emails) {
        Set<String> hs=new HashSet();
        for(String s:emails)
        {
            int count1=s.indexOf('@');
            String local=s.substring(0,count1);
            String rest=s.substring(count1);
             
            if(local.contains("+")){
                local=local.substring(0,local.indexOf('+'));
            }
            local=local.replaceAll(".","");
            s=local+rest;
            hs.add(s);
        }
        return hs.size();
    }
}

  

 

posted @   leagueandlegends  阅读(248)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示