File tree 1 file changed +10
-32
lines changed
1 file changed +10
-32
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
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 ();
8
4
}
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 ];
36
14
}
37
15
}
You can’t perform that action at this time.
0 commit comments