Skip to content

Latest commit

 

History

History
115 lines (82 loc) · 3.49 KB

0929._Unique_Email_Addresses.md

File metadata and controls

115 lines (82 loc) · 3.49 KB

929. Unique Email Addresses

难度: Easy

刷题内容

原题链接

内容描述

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 - 时间复杂度: O(N * max(len(email)))- 空间复杂度: O(max(len(email)))******

sb题没什么好说的

class Solution(object):
    def numUniqueEmails(self, emails):
        """
        :type emails: List[str]
        :rtype: int
        """
        res = set()
        for email in emails:
            local, domain = email.split('@')[0], email.split('@')[1]
            local = local.split('+')[0]
            new_email = ''.join([c for c in local if c != '.']) + '@' + domain
            res.add(new_email)
        return len(res)

思路 2 - 时间复杂度: O(N * max(len(email)))- 空间复杂度: O(max(len(email)))******

按照要求, local name 中的 '.' 可以被忽略 'leetcode' == 'leet.code' local name 中的 '+' 出现的地方其后面的字符全部忽略。

类似于注释一样。

domain name 没做额外说明。

明白了规则就很容易了:

  1. 按 '@' 分割字符串。

  2. 左边的部分是 local name 右边的是domain name,domain name 不用管。

  3. 将local name 中的 '.' 忽略。

  4. 将local name 中 '+' 后面的字符忽略。

  5. 可以直接用字符串的 replace 方法。

  6. 可以用正则。

class Solution(object):
    def numUniqueEmails(self, emails):
        """
        :type emails: List[str]
        :rtype: int
        """
        ignore = re.compile(r'\+.*')
        res = set()
        for email in emails:
            local, domain = email.split('@')[0], email.split('@')[1]
            local = re.sub(ignore, '', local).replace('.', '')
            new_email = local + '@' + domain
            res.add(new_email) 
        return len(res)

写个一行吧

class Solution(object):
    def numUniqueEmails(self, emails):
        """
        :type emails: List[str]
        :rtype: int
        """
        return len(set([email.split('@')[0].split('+')[0].replace('.', '') + '@' + email.split('@')[1] for email in emails]))