leetcode (go language) unique e-mail address

Each e-mail by a local name and a domain name, separated with the @ symbol.
For example, in [email protected] in, alice is a local name and leetcode.com is the domain name.
In addition to lowercase letters, emails which may also contain '.' Or '+'.
If you add a dot between certain characters in the local name part of the e-mail address in the ( '.'), Then to where the e-mail sent will be forwarded to the same address in the local name without points. For example, " [email protected] " and " [email protected] " will be forwarded to the same email address. (Note that this rule does not apply to the domain name.)
If you add in the local name plus ( '+'), all the content will be the first plus sign behind ignored. This allows some e-mail filters, such as [email protected] will be forwarded to [email protected] . (Again, this rule does not apply to the domain name.)
You can use these two rules at the same time.
Given a list of e-mail emails, we'll send an e-mail to each address list. The actual different address to receive mail how much?

Example:
Input: [ " [email protected] ", " [email protected] ", " [email protected] "]
Output: 2
Explanation: actually received the message that " [email protected] " and " [email protected] ."

Tip:
. 1 <= emails [i] .length <= 100
. 1 <= emails.length <= 100
per blocked emails [i] and only contains an '@' character.

package main

import (
    "fmt"
    "strings"
)

func numUniqueEmails(emails []string) int {
    set := make(map[string]bool)
    for _, email := range emails {
        strs := strings.Split(email, "@")
        ePref := strs[0]
        eSuffix := strs[1]
        actPref := ""
        for _, ich := range ePref {
            ch := fmt.Sprintf("%c", ich)
            if ch == "+" {
                break
            }

            if ch != "." {
                actPref += ch
            }
        }

        set[actPref+"@"+eSuffix] = true
    }
    return len(set)
}

func main() {
    emails := make([]string, 0)
    emails = append(emails, "[email protected]", "[email protected]", "[email protected]")
    fmt.Println(numUniqueEmails(emails))
}

The following program output,

8982195-afaacfb8c603be7d.png
image.png

Guess you like

Origin blog.csdn.net/weixin_34080903/article/details/91031578
Recommended