[LeetCode] 929. Unique Email Addresses 独特的邮件地址

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

For example, in?[email protected],?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,?"[email protected]"?and?"[email protected]"?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?[email protected]?will be forwarded to?[email protected].? (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: ["[email protected]","[email protected]","[email protected]"]
Output: 2
Explanation:?"[email protected]" and "[email protected]" actually receive mails

Note:

  • 1 <= emails[i].length?<= 100
  • 1 <= emails.length <= 100
  • Each?emails[i]?contains exactly one?‘@‘?character.
  • All local and domain names are non-empty.
  • Local names do not start with a?‘+‘?character.

博主正在刷题的时候,突然朋友圈刷出了科比坠机的消息,惊的下巴都掉了,忙看了下日期,不是四月一啊,于是疯狂的 google,中文搜不到任何相关的消息,于是搜英文 Kobe Bryant,结果真的有坠机消息,而且是几分钟前刚发布的,渐渐的很多微信群里都开始讨论了,连 wiki 上都更新了,随着越来越多的媒体确认这一个消息,心情越来越沉重了。算起来了,在博主最早关注 NBA 的时候,科比就当红球星,二十年的光辉岁月,五座总冠军戒指,甚至退役后还获得过奥斯卡小金人,年仅四十一岁,本来是要续写另一段传奇人生,就这么的走了?人生无常啊,你永远不知道意外和明天哪一个先到来,能平平安安的活着就已经是万幸了。RIP,一路走好,科比,愿天堂没有直升机。下面带着沉重的心情来做题吧,这道题是关于邮件的,邮件名里可能会有两个特殊符号,点和加号,对于点采取直接忽略的做法,对于加号则是忽略其后面所有的东西,现在问我们有多少个不同的邮箱。没有太多的技巧,就直接遍历一下所有的字符,遇到点直接跳过,遇到 ‘+‘ 或者 ‘@‘ 直接 break 掉。注意这里其实有个坑,就是域名中也可能有点,而这个点是不能忽略的,所以要把 ‘@‘ 及其后面的域名都提取出来,连到之前处理好的账号后面,一起放到一个 HashSet 中,利用其可以去重复的特性,最终剩余的个数即为所求,参见代码如下:

class Solution {
public:
    int numUniqueEmails(vector<string>& emails) {
        unordered_set<string> st;
        for (string email : emails) {
            string name;
            for (char c : email) {
                if (c == '.') continue;
                if (c == '+' || c == '@') break;
                name.push_back(c);
            }
            name += email.substr(email.find('@'));
            st.insert(name);
        }
        return st.size();
    }
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/929

参考资料:

https://leetcode.com/problems/unique-email-addresses/

https://leetcode.com/problems/unique-email-addresses/discuss/317207/C%2B%2B-Concise-Solution

https://leetcode.com/problems/unique-email-addresses/discuss/186798/Java-7-liner-with-comment.

LeetCode All in One 题目讲解汇总(持续更新中...)

原文地址:https://www.cnblogs.com/grandyang/p/12235380.html

时间: 2024-10-14 19:59:22

[LeetCode] 929. Unique Email Addresses 独特的邮件地址的相关文章

[leetcode] 929. Unique Email Addresses (easy)

统计有几种邮箱地址. 邮箱名类型:[email protected] 规则:1. local中出现"."的,忽略. a.bc=abc 2. local中出现"+"的,+以及之后的local全部忽略. a+bc=a 思路: 利用set存,水题没啥好说的 Runtime: 20 ms, faster than 96.66% of C++ online submissions for Unique Email Addresses. class Solution { pub

LeetCode 929 Unique Email Addresses 解题报告

题目要求 Every email consists of a local name and a domain name, separated by the @ sign. For example, in [email protected], alice is the local name, and leetcode.com is the domain name. If you add periods ('.') between some characters in the local name 

Leetcode-929 Unique Email Addresses(独特的电子邮件地址)

1 class Solution 2 { 3 public: 4 int numUniqueEmails(vector<string>& emails) 5 { 6 for(int i = 0;i < emails.size();i ++) 7 { 8 for(int j = 0;j < emails[i].size();j ++) 9 { 10 if(emails[i][j]=='@') 11 { 12 for(int k = 0;k < j;k ++) 13 {

929. Unique Email Addresses

Algorithm 做一个 leetcode 的算法题 Unique Email Addresses https://leetcode.com/problems/unique-email-addresses/ 1)problem 929. Unique Email Addresses Every email consists of a local name and a domain name, separated by the @ sign. For example, in [email pro

【leetcode】929. Unique Email Addresses

题目如下: Every email consists of a local name and a domain name, separated by the @ sign. For example, in [email protected], alice is the local name, and leetcode.com is the domain name. Besides lowercase letters, these emails may contain '.'s or '+'s.

[Swift Weekly Contest 108]LeetCode929. 独特的电子邮件地址 | Unique Email Addresses

Every email consists of a local name and a domain name, separated by the @ sign. For example, in [email protected], alice is the local name, and leetcode.com is the domain name. Besides lowercase letters, these emails may contain '.'s or '+'s. If you

LeetCode - Unique Email Addresses

Every email consists of a local name and a domain name, separated by the @ sign. For example, in [email protected], alice is the local name, and leetcode.com is the domain name. Besides lowercase letters, these emails may contain '.'s or '+'s. If you

[LeetCode] 288.Unique Word Abbreviation 独特的单词缩写

An abbreviation of a word follows the form <first letter><number><last letter>. Below are some examples of word abbreviations: a) it --> it (no abbreviation) 1 b) d|o|g --> d1g 1 1 1 1---5----0----5--8 c) i|nternationalizatio|n --&

LeetCode 804. Unique Morse Code Words

804. Unique Morse Code Words International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to &