LeetCode: 929. Unique Email Addresses / 不同的电子邮件地址

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?


理解一下:
一个 邮件地址的数组 emails = [String]()
筛选出其中不同的邮件地址. 有几种情况被判定为相同邮件地址.

举例子一个邮箱地址: myEmail@qq.com
其中@前面的部分 myEmail部分. 可能包含 ('.'), ('+')的符号.

('.') 的规则
alicez@leetcode.comalice.z@leetcode.com 相同.
也就是忽略 @ 符号前面部分的 . 符号

('+') 的规则
m.y+name@email.commy@email.com 相同
也就是忽略掉 + 符号 到 @ 符号之间的所有字符

如果 . 符号, 在 @ 符号后面则不忽略 . 符号

例子:

Input:
 ["test.email+alex@leetcode.com",
  "test.e.mail+bob.cathy@leetcode.com",
  "testemail+david@lee.tcode.com"]
Output: 2

Note:

1 <= emails[i].length <= 100
1 <= emails.length <= 100
Each emails[i] contains exactly one '@' character.

我的写法

class Solution {
    func numUniqueEmails(_ emails: [String]) -> Int {
        var email_dict = [String: Bool]()
        for (_, email) in emails.enumerated() {
            var email_key = ""
            var can_offset = true
            var before_point = true
            for (offset, element) in email.enumerated() {
                if 0 == offset, element == "+" { break }
                if element == "+" { can_offset = false }
                if element == "@" {
                    can_offset = true
                    before_point = false
                }
                if "." != element, can_offset {
                    email_key.append(element)
                }
                
                if "." == element, false == before_point {
                    email_key.append(element)
                }
            }
            if !email_key.isEmpty {
                email_dict[email_key] = true
            }
        }
        return email_dict.count
    }
}

LeetCode地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容