Unique Email Addresses

Unique Email Addresses

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?

Example 1:

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:

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
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值