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]" and "[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]. (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 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.
上述问题本质上是对字符串的处理问题,需要用到字符串的分割,合并,替换等操作。难度本身不大,只是为了熟悉字符串操作而已。所以本人首先了一个效率不怎么高的解法,拆分字符串数组,对每个"邮箱地址"进行处理。代码如下所示:
func numUniqueEmails(emails []string) int {
if emails == nil {
return 0
}
actual := make([]string, 0)
for _, email := range emails {
local := strings.Split(email, "@")[0]
domain := strings.Split(email, "@")[1]
if strings.Index(local, "+") != -1 {
local = strings.Split(local, "+")[0]
}
local = strings.Replace(local, ".", "", -1)
newEmail := fmt.Sprintf("%s@%s", local, domain)
if !isInActual(actual, newEmail) {
actual = append(actual, newEmail)
}
}
return len(actual)
}
func isInActual(actual []string, str string) bool {
isIn := false
for _, item := range actual {
if item == str {
isIn = true
}
}
return isIn
}
这个解法虽然搞定了需求,但是有很多多余的操作,比如判断是否是重复的字符串的时候,其实没必要通过函数调用isInActual,去一次次的遍历整个列表。这样的操作是O(n^2)时间复杂度。而golang本身提供了map这样的数据结构可以去重,所以代码可以再次简化:
func numUniqueEmails(emails []string) int {
if emails == nil {
return 0
}
e_map := make(map[string]int)
for idx, email := range emails {
local := strings.Split(email, "@")[0]
domain := strings.Split(email, "@")[1]
if strings.Index(local, "+") != -1 {
local = strings.Split(local, "+")[0]
}
local = strings.Replace(local, ".", "", -1)
newEmail := fmt.Sprintf("%s@%s", local, domain)
if _, ok:= e_map[newEmail]; !ok {
e_map[newEmail] = idx
}
}
return len(e_map)
}
整个解法比上个解法整整快了一倍,时间比较如下:
欢迎大家进行改进,希望得到更快的解法。
leetcode上有个哥们用了比较复杂的表达式进行切分,也得到了结果。代码比较简短。但是可读性不太好,而且比我的第二个解法还慢了一些。代码示例如下:
func numUniqueEmails(emails []string) int {
m := make(map[string]int)
for i := 0; i < len(emails); i++ {
parsedEmail := strings.Join(strings.Split(emails[i][0:strings.Index(emails[i], "+")-1], "."), "") + emails[i][strings.Index(emails[i], "@"):]
if _, ok := m[parsedEmail]; ok {
continue
}
m[parsedEmail] = i
}
result := len(m)
return result
}