Every email consists of a local name and a domain name, separated by the @ sign.python
For example, in alice@leetcode.com
, alice
is the local name, and leetcode.com
is the domain name.markdown
Besides lowercase letters, these emails may contain '.'
s or '+'
s.app
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.)dom
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.)ide
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?this
Example 1:code
Input: ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"] Output: 2 Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails
Note:leetcode
1 <= emails[i].length <= 100
1 <= emails.length <= 100
emails[i]
contains exactly one '@'
character.題目描述:大概是給你個郵箱的列表,給了一個 local name
的命名規則,問若是同時向這些郵箱中發送郵件,有多少不一樣郵箱可以實際收到這些郵件。rem
題目分析:挺水的一道題,首先,郵箱是由local name
+ @
+ domain name
組成。咱們從題意能夠知道給出 local name
的兩條命名規則:
.
直接無視。例如 alice.z@leetcode.com
和 alicez@leetcode.com
是等價的關係+
,忽略 +
後面的內容。例如 m.y+name@email.com
和 my@email.com
是等價的關係咱們考慮用集合進行去重操做,同一郵箱收到多條郵件只能算作一次。枚舉郵箱列表中的郵箱地址,根據 local name
的命名規則,經過 split
函數分割,將真實郵箱存入集合中,而後計算出的集合中的元素個數即爲咱們所求的郵箱數。
python
代碼:
class Solution(object): def numUniqueEmails(self, emails): """ :type emails: List[str] :rtype: int """ real_email = set() for i in range(len(emails)): x = emails[i] s = str(x.split("+")[0].replace('.','') + '@' + x.split("@")[1]) real_email.add(s) return len(real_email)
C++
代碼:
class Solution { public: int numUniqueEmails(vector<string>& emails) { set<string> x; for(int i = 0; i < emails.size(); i++){ if(emails[i].find('.') != string::npos){ emails[i].erase(remove(emails[i].begin(), find(emails[i].begin(), emails[i].end(),'@'), '.'), find(emails[i].begin(), emails[i].end(), '@')); } if(emails[i].find('+') != string::npos){ emails[i].erase(find(emails[i].begin(), find(emails[i].begin(), emails[i].end(), '@'), '+'), find(emails[i].begin(), emails[i].end(), '@')); } x.insert(emails[i]); } return x.size(); } };