LeetCode 929: Unique Email Addresses

問題:

Every email consists of a local name and a domain name, separated by the @ sign.
For example, in alice@leetcode.comalice 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? 

Example:

Input: ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]Output: 2Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails

解法:
這題的題目比較長
是在說”.”跟”+”對email位置的影響
“.”的話是省略不看
“+”則是一遇到後面都省略不看

先用”@”分離local name 跟 domain name
之後處理local name中”.”跟”+”

程式碼:

func numUniqueEmails(emails []string) int {    address := make(map[string]bool)	for _, item := range emails {		domain := strings.Split(item, "@")		// .		localName := ""		temp := strings.Split(domain[0], ".")		for _, s := range temp {			localName += s		}		// +		localName = strings.Split(localName, "+")[0]		localName += "@" + domain[1]		if _, ok := address[localName]; !ok {			address[localName] = true		}	}    return len(address)}

沒有留言:

張貼留言

About

努力在程式的大海
用力的揮動雙手
找出屬於自己的航線

Blog Archive

Traffic