Split each address, remove the local part after plus, delete local dots, then combine it with the unchanged domain in a set.
Complexity
O(total characters) time and space.
Count distinct destination addresses after applying local-name normalization rules.
Split each address, remove the local part after plus, delete local dots, then combine it with the unchanged domain in a set.
O(total characters) time and space.
package main
import "strings"
func numUniqueEmails(emails []string) int {
unique := map[string]bool{}
for _, email := range emails {
parts := strings.SplitN(email, "@", 2)
local := strings.SplitN(parts[0], "+", 2)[0]
local = strings.ReplaceAll(local, ".", "")
unique[local+"@"+parts[1]] = true
}
return len(unique)
}
class Solution:
def numUniqueEmails(self, emails: list[str]) -> int:
unique: set[str] = set()
for email in emails:
local, domain = email.split("@")
unique.add(local.split("+")[0].replace(".", "") + "@" + domain)
return len(unique)
function numUniqueEmails(emails: string[]): number {
const unique = new Set<string>();
for (const email of emails) { const [local, domain] = email.split("@"); const normalized = local.split("+")[0].replaceAll(".", ""); unique.add(normalized + "@" + domain); }
return unique.size;
}