1632. 电子邮件组的个数

1632. 电子邮件组的个数

中文English

给定一个n个邮件地址字符串的数组.
求出电子邮件组的数量,每个组应具有多个电子邮件地址(地址可以重复)。如果两个字符串在转换后具有相同的值,则它们位于同一组中。

邮件地址处理时遵循以下规则:

  1. 在字符`'@'之前忽略所有的字符 '.'.
  2. 在字符`'@'之前忽略字符 '+'以及它后面的所有子字符串.

样例

样例1

输入: emails = ["abc.bc+c+d@jiuzhang.com", "abcbc+d@jiuzhang.com", "abc.bc.cd@jiuzhang.com"]
输出: 1
解释:
这里只有一个email group. 是 "abcbc@jiuzhang.com".

样例2

输入: emails = ["abc.b+c+d@jiuzhang.com", "abcbc+d@jiuzhang.com", "abc.bc.cd@jiuzhang.com"]
输出: 0
解释:
这里不存在email group.

注意事项

电子邮件组至少有两个相同的电子邮件地址

class Solution:
    """
    @param emails: Original email
    @return: Return the count of groups which has more than one email address in it.
    """
    def countGroups(self, emails):
        # Write your code here
        if not emails: return 0

        emails_dict = {}
        count = 0

        #首先先筛一遍
        for email in emails:
            temp_email = ''
            index = -1
            split_email = email.split('@')
            email, later_email = split_email[0], split_email[1]
            length = len(email)

            while index < length - 1:
                index += 1
                if email[index] == '.':
                    continue
                elif email[index] == '+':
                    right = index + 1 
                    while right < length:
                        if email[right] == '+':
                            break
                        right += 1
                    
                    #更新
                    index = right - 1
                else:
                    temp_email += email[index]

            if temp_email + '@' + later_email not in emails_dict.keys():
                emails_dict[temp_email + '@' + later_email] = 1 
            else:
                emails_dict[temp_email + '@' + later_email] += 1
        
        #判断
        for val in emails_dict.values():
            if val > 1:
                count += 1 
        
        return count

 

posted @ 2020-12-19 18:42  风不再来  阅读(177)  评论(0编辑  收藏  举报