(?=exp):零宽度正预测先行断言(?<=exp):零宽度正回顾后发断言(?!exp):零宽度负预测先行断言(?<!exp):零宽度负回顾后发断言每个 有效电子邮件地址 都由一个 本地名 和一个 域名 组成,以 '@' 符号分隔。除小写字母之外,电子邮件地址还可以含有一个或多个 '.' 或 '+' 。
例如,在 [email protected]中, alice 是 本地名 ,而 leetcode.com 是 域名 。
如果在电子邮件地址的 本地名 部分中的某些字符之间添加句点('.'),则发往那里的邮件将会转发到本地名中没有点的同一地址。请注意,此规则 不适用于域名 。
例如,"[email protected]” 和 “[email protected]” 会转发到同一电子邮件地址。
如果在 本地名 中添加加号('+'),则会忽略第一个加号后面的所有内容。这允许过滤某些电子邮件。同样,此规则 不适用于域名 。
例如 [email protected] 将转发到 [email protected]。
可以同时使用这两个规则。
给你一个字符串数组 emails,我们会向每个 emails[i] 发送一封电子邮件。返回实际收到邮件的不同地址数目。
示例 1:
输入:emails = ["[email protected]","[email protected]","[email protected]"]
输出:2
解释:实际收到邮件的是 "[email protected]" 和 "[email protected]"。
var numUniqueEmails = function(emails) { // split 实现
const n = emails.length, h = new Set()
for (let i = 0; i < n; i++) {
const [local, domain] = emails[i].split('@')
h.add(local.split('+')[0].replace(/\./g, '') + '@' + domain)
}
return h.size
}; var numUniqueEmails = function(emails) { // slice 或 substring 或 substr 实现
const n = emails.length, h = new Set()
for (let i = 0; i < n; i++) {
const j = emails[i].indexOf('@'), l = emails[i].slice(0, j), r = emails[i].slice(j), k = l.indexOf('+')
h.add((k > - 1 ? l.slice(0, k) : l).replaceAll('.', '') + r)
}
return h.size
}; function numUniqueEmails(emails: string[]): number {
const h = new Set()
for (const email of emails) {
const i = email.indexOf('@'), l = email.substring(0, i), r = email.substring(i), j = l.indexOf('+')
h.add((j > -1 ? l.substring(0, j) : l).replace(/\./g, '') + r)
}
return h.size
}; class Solution {
function numUniqueEmails($emails) {
$h = array();
foreach($emails as $email) {
$i = strpos($email, '@');
$l = substr($email, 0, $i);
$r = substr($email, $i);
$j = strpos($l, '+');
$h[str_replace('.', '', $j > -1 ? substr($l, 0, $j) : $l) . $r] = 1;
}
return count(array_keys($h));
}
} func numUniqueEmails(emails []string) int { // Split 实现
type void struct{}
var value void
h := map[string]void{}
for _, email := range emails {
s := strings.SplitN(email, "@", 2)
l, r := s[0], s[1]
s = strings.SplitN(l, "+", 2)
l = s[0]
l = strings.ReplaceAll(l, ".", "")
h[l + "@" + r] = value
}
return len(h)
} func numUniqueEmails(emails []string) int { // Index 实现
type void struct{}
var value void
h := map[string]void{}
for _, email := range emails {
i := strings.IndexByte(email, '@')
l, r := email[:i], email[i:]
j := strings.IndexByte(l, '+')
if j > -1 {
l = email[:j]
}
l = strings.ReplaceAll(l, ".", "")
h[l + r] = value
}
return len(h)
} class Solution { // split 实现
public int numUniqueEmails(String[] emails) {
int n = emails.length;
HashSet<String> h = new HashSet<String>();
for (int i = 0; i < n; i++) {
String[] a = emails[i].split("@", 2);
String l = a[0];
String r = a[1];
a = l.split("\\+", 2);
l = a[0];
h.add(l.replaceAll("\\.", "") + "@" + r);
}
return h.size();
}
} class Solution { // indexOf 实现
public int numUniqueEmails(String[] emails) {
int n = emails.length;
HashSet<String> h = new HashSet<String>();
for (int i = 0; i < n; i++) {
int j = emails[i].indexOf("@");
String l = emails[i].substring(0, j);
String r = emails[i].substring(j);
int k = l.indexOf("+");
if (k > -1) l = l.substring(0, k);
h.add(l.replaceAll("\\.", "") + r);
}
return h.size();
}
} class Solution: # split 实现
def numUniqueEmails(self, emails: List[str]) -> int:
h = set()
for _, email in enumerate(emails):
l1 = email.split('@', 2)
l, r = l1[0], l1[1]
l2 = l.split('+', 2)
l = l2[0]
l = l.replace('.', '')
h.add(l + '@' + r)
return len(h) class Solution: # find 实现
def numUniqueEmails(self, emails: List[str]) -> int:
h = set()
for _, email in enumerate(emails):
i = email.find("@")
l, r = email[:i], email[i:]
j = l.find("+")
if j > -1 : l = l[:j]
l = l.replace('.', '')
h.add(l + r)
return len(h) var numUniqueEmails = function(emails) {
const n = emails.length, h = new Set()
for (let i = 0; i < n; i++) {
h.add(emails[i].replace(/(\.|\+.*)(?=.*@)/g, ''))
}
return h.size
}; var numUniqueEmails = function(emails) {
const n = emails.length, h = new Set()
for (let i = 0; i < n; i++) {
h.add(emails[i].replace(/\.(?=.*@)|\+[^@]*/g, ''))
}
return h.size
}; function numUniqueEmails(emails: string[]): number {
const h = new Set()
for (const email of emails) {
h.add(email.replace(/(\.|\+.*)(?=.*@)/g, ''))
}
return h.size
}; class Solution {
function numUniqueEmails($emails) {
$h = array();
foreach($emails as $email) {
$h[preg_replace('/(\.|\+.*)(?=.*@)/', '', $email)] = 1;
}
return count(array_keys($h));
}
} class Solution {
public int numUniqueEmails(String[] emails) {
int n = emails.length;
HashSet<String> h = new HashSet<String>();
for (int i = 0; i < n; i++) {
h.add(emails[i].replaceAll("(\\.|\\+.*)(?=.*@)", ""));
}
return h.size();
}
} class Solution:
def numUniqueEmails(self, emails: List[str]) -> int:
h = set()
for _, email in enumerate(emails):
h.add(re.sub(r'(\.|\+.*)(?=.*@)', '', email))
return len(h)