108th LeetCode Weekly Contest 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.

没啥好说的,就是个简单的模拟。可以考虑@前后两部分

复制代码
class Solution {
public:
    int numUniqueEmails(vector<string>& emails) {
        set<string>Set;
        int len = emails.size();
        string strlen="";
        string x="";
        int flag=0;
        int j=0;
        int y=0;
        int Jstr;
        for(int i=0;i<len;i++){

            strlen = emails[i];

            flag = 0;
            j = 0;
            y = 0;
            x = "";
            Jstr = strlen.length();
            while(strlen[j]!='@'){
                 j++;
            }

            while(y<=j){
                if(strlen[y]=='.'){
                    y++;
                }
                if(strlen[y]=='+'){
                    break;
                }
                x+=strlen[y];
                y++;
            }
            while(j<Jstr){
                x+=strlen[j];
                j++;
            }
           //cout<<x<<endl;
            Set.insert(x);
        }
        return Set.size();
    }
};
复制代码

 

posted @   樱花落舞  阅读(229)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
历史上的今天:
2017-10-28 Python 起步 多版本共存配置
2017-10-28 栈与队列试题中的操作代码
点击右上角即可分享
微信分享提示