File tree Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ private String getFormattedEmail (String email ) {
3
+ String [] arr = email .split ("@" );
4
+ String localName = arr [0 ];
5
+ String domainName = arr [1 ];
6
+
7
+ // Only keep the first part of "+" sign
8
+ String [] arrLocalWithPlus = localName .split ("\\ +" );
9
+ localName = arrLocalWithPlus [0 ];
10
+
11
+ // Replace "."/dots
12
+ localName = localName .replace ("." , "" );
13
+
14
+ return localName + "@" + domainName ;
15
+ }
16
+
17
+ public int numUniqueEmails (String [] emails ) {
18
+ Set <String > uniqueEmails = new HashSet <>();
19
+ for (String email : emails ) {
20
+ String formattedEmail = getFormattedEmail (email );
21
+ uniqueEmails .add (formattedEmail );
22
+ }
23
+ return uniqueEmails .size ();
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func getFormattedEmail( from email: String ) -> String {
3
+ let arr = email. split ( separator: " @ " )
4
+
5
+ var localNameInitial = arr [ 0 ]
6
+ let domainName = arr [ 1 ]
7
+
8
+ // Ignore everything from first + (plus) sign
9
+ let arrLocalNameSplitPlus = localNameInitial. split ( separator: " + " )
10
+ guard let localNameWithoutPlus = arrLocalNameSplitPlus. first else { return " " }
11
+
12
+ // Remove . (dot) sign
13
+ let localName = localNameWithoutPlus. replacingOccurrences ( of: " . " , with: " " )
14
+
15
+ return " \( localName) @ \( domainName) "
16
+ }
17
+
18
+ func numUniqueEmails( _ emails: [ String ] ) -> Int {
19
+ var uniqueEmails : Set < String > = Set ( )
20
+
21
+ emails. forEach { email in
22
+ let formattedEmail = getFormattedEmail ( from: email)
23
+ uniqueEmails. insert ( formattedEmail)
24
+ }
25
+
26
+ return uniqueEmails. count
27
+ }
28
+ }
You can’t perform that action at this time.
0 commit comments