Daily Algorithm----929. Unique Email Address----2022/06/04

1. Question description

Every valid email address consists of a local name and a domain name, separated by the '@' symbol. In addition to lowercase letters, email addresses can contain one or more '.' or '+' characters.

For example, in [email protected], alice is the local name and leetcode.com is the domain name.
If you add a period ('.') between certain characters in the local name portion of an email address, mail sent there will be forwarded to the same address without the period in the local name. Note that this rule does not apply to domain names.

For example, "[email protected]" and "[email protected]" are forwarded to the same email address.
If you add a plus sign ('+') to a local name, everything after the first plus sign is ignored. This allows filtering of certain emails. Again, this rule does not apply to domain names.

For example [email protected] will be forwarded to [email protected].
You can use both rules at the same time.

Given an array of string emails, we will send an email to each emails[i]. Returns the number of distinct addresses to which mail was actually received.

Source: LeetCode
Link: https://leetcode.cn/problems/unique-email-addresses
The copyright belongs to LeetCode Network. For commercial reprinting, please contact the official authorizer. For non-commercial reprinting, please indicate the source.

2. Example

Example 1:

输入:emails = ["[email protected]","[email protected]","[email protected]"]
输出:2
解释:实际收到邮件的是 "[email protected]" 和 "[email protected]"。

Example 2:

输入:emails = ["[email protected]","[email protected]","[email protected]"]
输出:3

hint:

  • 1 <= emails.length <= 100
  • 1 <= emails[i].length <= 100
  • emails[i] consists of lowercase English letters, '+', '.' and '@'
  • Each email[i] contains exactly one '@' character
  • All local names and domain names are not empty
  • The local name does not start with the '+' character

Source: LeetCode
Link: https://leetcode.cn/problems/unique-email-addresses
The copyright belongs to LeetCode Network. For commercial reprinting, please contact the official authorizer. For non-commercial reprinting, please indicate the source.

3. Ideas

Divided into three situations

  • When encountered ., overwrite
  • When encountered +, go find @the location behind
  • When you encounter @it, you don’t have to look for it anymore.

4. Problems encountered

none.

5. Specific implementation code

Code written by myself

func numUniqueEmails(emails []string) int {
    
    
	diffEmailsCount := 0
	diffEmails := make(map[string]bool)
	for i:=0;i<len(emails);i++{
    
    
		targetString := ""

		for j:=0;j<len(emails[i]);j++{
    
    
			if emails[i][j] == 43{
    
     //=='+'
				index := findChar(j,emails[i])
				targetString = emails[i][0:j] + emails[i][index:]
				break
			}
			if emails[i][j] == 46{
    
     //=='.'
				emails[i] = emails[i][:j]+emails[i][j+1:]
			}
			if emails[i][j] == 64{
    
     //=='@'
				targetString = emails[i]
				break
			}
		}
		if !diffEmails[targetString] {
    
    
			diffEmailsCount ++
		}
		diffEmails[targetString] = true
	}
	return diffEmailsCount
}

func findChar(startIndex int,char string)int{
    
    
	for i:= startIndex;i<len(char);i++{
    
    
		if char[i] == 64{
    
    
			return i
		}
	}
	return -1
}

6. Official solution

func numUniqueEmails(emails []string) int {
    
    
    emailSet := map[string]struct{
    
    }{
    
    }
    for _, email := range emails {
    
    
        i := strings.IndexByte(email, '@')
        local := strings.SplitN (email[:i], "+", 2)[0] // 去掉本地名第一个加号之后的部分
        local = strings.ReplaceAll(local, ".", "")    // 去掉本地名中所有的句点
        emailSet[local+email[i:]] = struct{
    
    }{
    
    }
    }
    return len(emailSet)
}

Author: LeetCode-Solution Link
: https://leetcode.cn/problems/unique-email-addresses/solution/du-te-de-dian-zi-you-jian-di-zhi-by-leet-f178/Source
: LeetCode
copyright belongs to the author. For commercial reprinting, please contact the author for authorization. For non-commercial reprinting, please indicate the source.

strings.IndexByte : Find character position.
strings.SplitN : Split strings based on characters.
strings.ReplaceAll : Replace specific characters with other characters.

7 Source of question

leetCode


A day for improvement------swrici

Guess you like

Origin blog.csdn.net/Srwici/article/details/125107725