Skip to content

Commit fc52900

Browse files
authored
Merge pull request neetcode-gh#1442 from Mahim1997/dev/929_Java_Swift
929. Unique Email Addresses
2 parents 11dacb6 + 59eb3d9 commit fc52900

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

java/929-Unique-Email-Addresses.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}

0 commit comments

Comments
 (0)