资讯专栏INFORMATION COLUMN

[LeetCode] 929. Unique Email Addresses

amuqiao / 1106人阅读

Problem

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?

Example 1:

Input: ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
Output: 2
Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails

Note:

1 <= emails[i].length <= 100
1 <= emails.length <= 100
Each emails[i] contains exactly one "@" character.

Solution
class Solution {
    public int numUniqueEmails(String[] emails) {
        Set set = new HashSet<>();
        for (String email: emails) {
            String[] parts = email.split("@");
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < parts[0].length(); i++) {
                char ch = parts[0].charAt(i);
                if (ch == ".") continue;
                else if (ch == "+") break;
                else sb.append(ch);
            }
            sb.append("@").append(parts[1]);
            set.add(sb.toString());
        }
        return set.size();
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72519.html

相关文章

  • Leetcode PHP题解--D2 929. Unique Email Addresses

    摘要:题目链接题目分析题目要求过滤重复的邮箱地址。最终返回不重复的用户名个数。域名部分则不进行处理。替换为空字符串。下标为用户名部分,下标为域名部分删去后面的所有字符。最后,用包住以上代码,在外面初始化数组,用去重,再该数组就完成了。 929. Unique Email Addresses 题目链接 929. Unique Email Addresses 题目分析 题目要求过滤重复的邮箱地址...

    xuhong 评论0 收藏0
  • 929-独特的电子邮件地址

    摘要:前言的第一题独特的电子邮件地址每封电子邮件都由一个本地名称和一个域名组成,以符号分隔。例如,和会转发到同一电子邮件地址。实现代码独特的电子邮件地址本地名称域名根据指定规则解析后的本地名称,先按加号切割字符串,然后替换使用去重 前言 Weekly Contest 108的第一题 独特的电子邮件地址: 每封电子邮件都由一个本地名称和一个域名组成,以@符号分隔。 例如,在 alice@le...

    IntMain 评论0 收藏0
  • 1.过滤邮箱地址

    摘要:题目初始算法测试提交优化算法利用的去重利用正则和是一个特殊字符所以需要转义符代表在之后的所有字符代表结尾代表或代表在全局用替代开始到结尾的字符和 1.题目Every email consists of a local name and a domain name, separated by the @ sign. For example, in alice@leetcode.com, ...

    caiyongji 评论0 收藏0
  • 1.过滤邮箱地址

    摘要:题目初始算法测试提交优化算法利用的去重利用正则和是一个特殊字符所以需要转义符代表在之后的所有字符代表结尾代表或代表在全局用替代开始到结尾的字符和 1.题目Every email consists of a local name and a domain name, separated by the @ sign. For example, in alice@leetcode.com, ...

    littleGrow 评论0 收藏0
  • Python-SQLALchemy

    摘要:因为是工作在一个内部,有时候我们可能不小心做了一些误删除的操作,可以回滚。我们先修改的用户名为,然后重新添加一个新,但是记住这个时候我们还没有。集合类型可以是各种合法类型,比如,但是默认集合是一个。 官方文档 Initialization # 检查是否已经安装以及版本号 >>> import sqlalchemy >>> sqlalchemy.__version__ ’1.1.4‘ ...

    kumfo 评论0 收藏0

发表评论

0条评论

amuqiao

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<