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 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.

2)answer

试题是希望过滤每个邮件中的【.】符号,忽略第一个【+】后面的所有内容。

在网上搜索到用Python的题解:

class Solution(object):
    def numUniqueEmails(self, emails):
        unique_emails = set()

        for email in emails:
            local_name, domain_name = email.split('@')
            local_name = local_name.replace('.', '')
            local_name = local_name.split('+')[0]
            modified_email = local_name + '@' + domain_name
            unique_emails.add(modified_email)

        return len(unique_emails)

设定一个集合变量,先是提取出邮件名与域名,然后过滤掉所有的【.】号。选择所有加号前的第一个索引值,然后把邮件名和域名合并在一起加入集合变量里。返回集合变量的数值就是实际发送数量。

3)solution

用C++做起来要复杂一点。

  • 1、提取【@】符号前面的内容,得到邮件名
  • 2、替换掉所有【.】符号,得到实际邮件名
  • 3、提取出【+】符号前的内容,得到发送的邮件名
  • 4、将邮件名与域名合并成实际发送的邮件地址
#include "pch.h"
#include <stdio.h>
#include <string>
#include <vector>
#include <set>
#include <iostream>
#include <algorithm>
using std::string;
using std::vector;
using std::set;

class Solution {
public:
    int numUniqueEmails(vector<string>& emails) {
        //集合中包含的元素值是唯一的。
        set<string> email_sets;
        for (string email : emails)
        {
            // 提取邮件名
            int pos = email.find('@');
            string local_name = email.substr(0, pos);
            // 过滤掉【.】符号
            // remove:从给定范围中消除指定值,而不影响剩余元素的顺序,并返回不包含指定值的新范围的末尾。
            // 从string中删除所有某个特定字符
            local_name.erase(std::remove(local_name.begin(), local_name.end(), '.'), local_name.end());
            // 提取【+】符号前的字符
            pos = local_name.find('+');
            local_name = local_name.substr(0, pos);
            // 提取【@】后的域名
            pos = email.find('@');
            string domain_name = email.substr(pos + 1);
            // 合并实际发送的邮件名称
            email = local_name + '@' + domain_name;
            // 写进集合中
            email_sets.insert(email);

        }
        // 返回集合大小
        return email_sets.size();
    }
};

int main()
{
    //vector初始化字符串
    vector<string> emails;

    emails.push_back("[email protected]");
    emails.push_back("[email protected]");
    emails.push_back("[email protected]");

    // 使用内容
    Solution nSolution;
    nSolution.numUniqueEmails(emails);
}

原文地址:https://www.cnblogs.com/17bdw/p/10358160.html

时间: 2024-10-12 17:01:12

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. Besides lowercase letters, these emails may contain?'.'s or?'+'s. If you

【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.

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 

[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-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 {

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 &

CodeForce 589A Email Aliases

Email Aliases Polycarp has quite recently learned about email aliases. Of course, he used to suspect that the case of the letters doesn't matter in email addresses. He also learned that a popular mail server in Berland bmail.com ignores dots (charact