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?
理解一下:
一個(gè) 郵件地址的數(shù)組 emails = [String]()
篩選出其中不同的郵件地址. 有幾種情況被判定為相同郵件地址.
舉例子一個(gè)郵箱地址: myEmail@qq.com
其中@前面的部分 myEmail部分. 可能包含 ('.'), ('+')的符號(hào).('.') 的規(guī)則
alicez@leetcode.com 與 alice.z@leetcode.com 相同.
也就是忽略 @ 符號(hào)前面部分的 . 符號(hào)('+') 的規(guī)則
m.y+name@email.com 與 my@email.com 相同
也就是忽略掉 + 符號(hào) 到 @ 符號(hào)之間的所有字符如果 . 符號(hào), 在 @ 符號(hào)后面則不忽略 . 符號(hào)
例子:
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
}
}