[LeetCode] 929. Unique Email Addresses unique e-mail address



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.



Bloggers are problems when the brush, brush out of the circle of friends suddenly Bryant crash news, scared jaw dropped, busy looked under the date is not April ah, so crazy google, Chinese search not see any relevant the message, then search English Kobe Bryant, the result of the crash really news, but also just released a few minutes ago, getting a lot of micro letter the group began to discuss, even on the wiki have been updated, as more and more media confirmed this to be a message, and I feel more and more heavy. Looking back, and at the time of the earliest bloggers concerned about the NBA, Bryant popular players, two decades of glory days, five championship rings, and even after retirement also won Oscars, only forty-one year old, could have been to continue to write another piece of the legendary life, so it's gone? Impermanence of life ah, you never know what tomorrow accident and a first come, can live in peace is already lucky. RIP, all the way, Bryant is willing to heaven no helicopters. The following questions with a heavy heart to do it, this question is about the message, the message may be in the name of two special symbols, and plus point for point to take a direct approach ignored for the plus sign is ignored behind all things, and now we ask how many different mailboxes we have. Not too many skills to traverse directly about all the characters encounter point skip encounter '+' or '@' direct break away. Note that in fact there is a pit, that is, the domain name may be a bit too, and this point can not be ignored, so it is a '@' and the back of the domain name are extracted, even before a good deal behind the account, put together a HashSet, the use of which may deduplication characteristics, the final remaining number is also desired, see the following code:


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 sync Address:

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



References:

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 topic explain summary (Update ...)

Guess you like

Origin www.cnblogs.com/grandyang/p/12235380.html