[Swift Weekly Contest 108]LeetCode929. 獨特的電子郵件地址 | 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]"
"[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]
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: ["[email protected]","[email protected]","[email protected]"]
Output: 2
Explanation: "[email protected]" and "[email protected]" actually receive mails
Note:
1 <= emails[i].length <= 100
1 <= emails.length <= 100
- Each
emails[i]
contains exactly one‘@‘
character.
每封電子郵件都由一個本地名稱和一個域名組成,以 @ 符號分隔。
例如,在 [email protected]
中, alice
是本地名稱,而 leetcode.com
是域名。
除了小寫字母,這些電子郵件還可能包含 ‘,‘
或 ‘+‘
。
如果在電子郵件地址的本地名稱部分中的某些字符之間添加句點(‘.‘
),則發往那裏的郵件將會轉發到本地名稱中沒有點的同一地址。例如,"[email protected]”
和 “[email protected]”
會轉發到同一電子郵件地址。 (請註意,此規則不適用於域名。)
如果在本地名稱中添加加號(‘+‘
),則會忽略第一個加號後面的所有內容。這允許過濾某些電子郵件,例如 [email protected]
將轉發到 [email protected]
。 (同樣,此規則不適用於域名。)
可以同時使用這兩個規則。
給定電子郵件列表 emails
,我們會向列表中的每個地址發送一封電子郵件。實際收到郵件的不同地址有多少?
示例:
輸入:["[email protected]","[email protected]","[email protected]"] 輸出:2 解釋:實際收到郵件的是 "[email protected]" 和 "[email protected]"。
提示:
1 <= emails[i].length <= 100
1 <= emails.length <= 100
- 每封
emails[i]
都包含有且僅有一個‘@‘
字符。
412ms
1 class Solution { 2 func numUniqueEmails(_ emails: [String]) -> Int { 3 var es:Set<String> = Set<String>() 4 for e in emails 5 { 6 //分割字符串 7 var s: Array = e.components(separatedBy: "@") 8 var str:String = String(s[0]) 9 //字符串替換 10 str = str.replacingOccurrences(of: ".", with: "") 11 //字符查找,返回字符索引 12 var ind = str.firstIndex(of: "+") ?? s[0].endIndex 13 if ind != str.endIndex 14 { 15 //截取子字符串 16 str = String(str[..<ind]) 17 } 18 //拼接字符串,Set添加用.insert 19 es.insert(str + "@" + s[1]) 20 } 21 return es.count 22 } 23 }
[Swift Weekly Contest 108]LeetCode929. 獨特的電子郵件地址 | Unique Email Addresses