[LeetCode] 929. Unique Email Addresses 獨特的郵件地址



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

For example, in alice@leetcode.comalice 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.
  • 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/4606334.html)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM