gpt4 book ai didi

929. Unique Email Addresses 独特的电子邮件地址

转载 作者:大佬之路 更新时间:2024-01-31 14:16:22 27 4
gpt4 key购买 nike

题目地址:https://leetcode.com/problems/unique-email-addresses/description/

题目描述

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

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

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

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

Itis 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、 1<=emails[i].length<=100;
2、 1<=emails.length<=100;
3、 Eachemails[i]containsexactlyone'@'character.;

题目大意

判断有多少个不重复的email.

邮箱的规则是这样的:域名部分不用管,只管用户名部分,用户名中如果包含有'.',那么把这个'.'给去掉;如果用户名中有'+',那么把加号以及后面的用户名部分全部去掉。

解题方法

set + 字符串操作

Python处理这种字符串题目简直不要太简单,这个题是周赛第一题,用了6分钟读完题目,然后提交通过了。

首先把'.'给替换掉,然后查找是不是包含'+',如果有的话,只保留其前面的部分就好了。

因为要去重,所以要使用一个set保存所有不重复的结果。

时间复杂度是O(N),空间复杂度是O(N)。

class Solution(object):
    def numUniqueEmails(self, emails):
        """
        :type emails: List[str]
        :rtype: int
        """
        eset = set()
        for email in emails:
            simper = self.simpifyEmail(email)
            eset.add(simper)
        return len(eset)
    
    def simpifyEmail(self, email):
        local, domain = email.split("@")
        local = local.replace('.', '')
        plus_i = local.find('+')
        if plus_i != -1:
            local = local[:plus_i]
        return local + "@" + domain

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

代码可以优化变短,使用replace代替find函数。

class Solution:
    def numUniqueEmails(self, emails):
        """
        :type emails: List[str]
        :rtype: int
        """
        res = set()
        for email in emails:
            name, domain = email.split("@")
            name = name.split("+")[0].replace(".", "")
            res.add(name + "@" + domain)
        return len(res)

1 2 3 4 5 6 7 8 9 10 11 12

参考资料

DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有

本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com