<easy>LeetCode Problem -- 929. 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?

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.

  • 分析:给出多个电子邮件地址,对其进行化简,找出有多少个不同的邮件地址。
    其中有如下三条规则:
    1.在’@'前出现‘.’,就将‘.’删除
    2.在’@'前出现‘+’,则忽视‘+’后面的字符
    3.对’@'后的字符不做任何替换

  • 思路一:对给出的邮件地址进行分段,分为‘+’(或‘@’)之前和 ‘@’之后的部分,对前半部分的‘.’进行删除,将后半部分与删除操作后的前半部分进行拼接,将拼接所得的string存入set中,最终set中的元素个数就是不同的电子邮件数目。

class Solution {
public:
    int numUniqueEmails(vector<string>& emails) {
        set<string> emails_match;
        string temp_str = "";
        int spilt_pos = 0, unique_pos = 0;
        for (int i = 0 ; i < emails.size(); i++) {
            unique_pos = emails[i].find('@');
            spilt_pos = unique_pos;
            if (emails[i].find('+') != -1) spilt_pos = emails[i].find('+');
            temp_str = emails[i].substr(0, spilt_pos);
            for (int j = 0; j < temp_str.size(); j++) {
                if (temp_str.find('.') == -1) break;
                int pos = temp_str.find('.', j);
                temp_str.erase(pos, 1);
                j = pos;
            }
            temp_str += emails[i].substr(unique_pos, emails[i].size() - unique_pos);
            emails_match.insert(temp_str);
        }
        return emails_match.size();
    }
};
  • 思路二:用remove()函数对替换‘.’操作进行优化
class Solution {
public:
    int numUniqueEmails(vector<string>& emails) {
        set<string> emails_match;
        string temp_str = "";
        int spilt_pos = 0, unique_pos = 0;
        for (int i = 0 ; i < emails.size(); i++) {
            unique_pos = emails[i].find('@');
            spilt_pos = unique_pos;
            if (emails[i].find('+') != -1) spilt_pos = emails[i].find('+');
            temp_str = emails[i].substr(0, spilt_pos);
            temp_str.erase(remove(temp_str.begin(), temp_str.end(), '.'), temp_str.end());
            temp_str += emails[i].substr(unique_pos, emails[i].size() - unique_pos);
            emails_match.insert(temp_str);
        }
        return emails_match.size();
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值