1.过滤邮箱地址

发布时间:2019-10-08 20:14:23编辑:auto阅读(2046)

    1.题目
    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. 初始算法
    class Solution:
        def numUniqueEmails(self, emails: List[str]) -> int:
            n = len(emails)
            final_list = []
            for i in range(0, n):
                split_list =  emails[i].split('@')
                local_name, domain_name = split_list[0].split('.'), split_list[1]
                local_name = ''.join(local_name).split('+')[0]
                final_name = local_name + '@' + domain_name
                if(final_name not in final_list):
                    final_list.append(final_name)        
            return len(final_list)
    测试提交 Runtime: 52 ms, faster than 55.04% of Python3 online submissions
    for Unique Email Addresses. Memory Usage: 13.2 MB, less than 5.79% of
    Python3 online submissions for Unique Email Addresses.
    1. 优化算法

    利用python的set去重

    class Solution:
        def numUniqueEmails(self, emails):
            """
            :type emails: List[str]
            :rtype: int
            """
            email_set = set()
            for email in emails:
                local_name,domain_name = email.split("@")
                local_name ="".join(local_name.split('+')[0].split('.'))
                email = local_name +'@' + domain_name
                email_set.add(email)
            return len(email_set)

    Js利用正则和Set

    const numUniqueEmails = emails => new Set(emails.map(mail => `${mail.split('@')[0].replace(/\+.*$|\./g, '')}@${mail.split('@')[1]}`)).size
    • is a specail char, so adds .

    .* means any character after +.
    $, in regex, it represents the end of string.
    | equals to or.
    . is also a special char, so adds .
    In the end of regex, g, global search, means finding all matches in input and replace them.
    In sum, replace the substring that after sign + or the char . with empty string.

    1.+是一个特殊字符所以需要转义符\
    2..*代表在+之后的所有字符
    3.$代表结尾
    4.|代表或
    5.replace(/\+.*$|\./g, '')代表在全局用''替代+开始到结尾的字符和.

关键字