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

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

For example, in [email protected], 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, "[email protected]" and "[email protected]" 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 [email protected] will be forwarded to [email protected]. (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]()
筛选出其中不同的邮件地址. 有几种情况被判定为相同邮件地址.

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

('.') 的规则
[email protected][email protected] 相同.
也就是忽略 @ 符号前面部分的 . 符号

('+') 的规则
[email protected][email protected] 相同
也就是忽略掉 + 符号 到 @ 符号之间的所有字符

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

例子:

Input:
 ["[email protected]",
  "[email protected]",
  "[email protected]"]
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地址

你可能感兴趣的:(LeetCode: 929. Unique Email Addresses / 不同的电子邮件地址)