929. Unique Email Addresses

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public int numUniqueEmails(String[] emails) {
Set<String> emailSet = new HashSet<>();
for (String email : emails) {
StringBuilder sb = new StringBuilder(email.length());
boolean inPlusRegion = false;
int i = 0;
for (; i < email.length(); i++) {
char c = email.charAt(i);
if (c == '@') {
break;
} else if (c == '.') {
continue;
} else if (c == '+') {
inPlusRegion = true;
} else if (!inPlusRegion) {
sb.append(c);
}
}
emailSet.add(sb.append(email, i, email.length()).toString());
}
return emailSet.size();
}
}

References

929. Unique Email Addresses