Skip to content

Commit 1a3fe56

Browse files
authored
Update Unique Email Addresses.java
1 parent c507cfe commit 1a3fe56

File tree

1 file changed

+10
-32
lines changed

1 file changed

+10
-32
lines changed

Easy/Unique Email Addresses.java

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,15 @@
11
class Solution {
22
public int numUniqueEmails(String[] emails) {
3-
Set<String> set = new HashSet<>();
4-
for (String email : emails) {
5-
set.add(formattedEmail(email));
6-
}
7-
return set.size();
3+
return Arrays.stream(emails).map(Solution::formatEmail).collect(Collectors.toSet()).size();
84
}
9-
10-
private String formattedEmail(String s) {
11-
StringBuilder sb = new StringBuilder();
12-
int i = 0;
13-
int n = s.length();
14-
boolean plusFound = false;
15-
while (i < n) {
16-
char c = s.charAt(i);
17-
if (c == '@') {
18-
break;
19-
}
20-
if (plusFound) {
21-
i++;
22-
continue;
23-
}
24-
else if (c == '+') {
25-
plusFound = true;
26-
}
27-
else if (c != '.') {
28-
sb.append(c);
29-
}
30-
i++;
31-
}
32-
while (i < n) {
33-
sb.append(s.charAt(i++));
34-
}
35-
return sb.toString();
5+
6+
private static String formatEmail(String email) {
7+
String localName = email.split("@")[0];
8+
return (localName.indexOf('+') != -1
9+
? localName.substring(0, localName.indexOf('+'))
10+
: localName)
11+
.replaceAll("\\.", "")
12+
+ "@"
13+
+ email.split("@")[1];
3614
}
3715
}

0 commit comments

Comments
 (0)