LeetCode: 929. Unique Email Addr
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.com 与 alice.z@leetcode.com 相同.
也就是忽略 @ 符号前面部分的 . 符号('+') 的规则
m.y+name@email.com 与 my@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
}
}