package main import ( "fmt" "strings" ) func numUniqueEmails(emails []string) int { var dic map[string]int dic = make(map[string]int) for _, s := range emails { strArr := strings.Split(s, "@") localname := strArr[0] domainname := strArr[1] plusIndex := strings.Index(localname, "+") if plusIndex > 0 { localname = localname[0:plusIndex] } localname = strings.Replace(localname, ".", "", -1) realmail := localname + "@" + domainname _, ok := dic[realmail] if ok { //found realmail } else { dic[realmail] = 1 } } return len(dic) } func main() { emails := []string{"test.email+alex@leetcode.com", "test.e.mail+bob.cathy@leetcode.com", "testemail+david@lee.tcode.com"} num := numUniqueEmails(emails) fmt.Println(num) }
public class Solution { public int NumUniqueEmails(string[] emails) { var dic = new Dictionary<string, int>(); foreach (var email in emails) { var mails = email.Split('@'); var localname = mails[0]; var domainname = mails[1]; var plusIndex = localname.IndexOf('+'); if (plusIndex >= 0) { localname = localname.Substring(0, plusIndex); } localname = localname.Replace(".", ""); var realmail = localname + "@" + domainname; if (!dic.ContainsKey(realmail)) { dic.Add(realmail, 1); } } return dic.Count; } }