forked from anishLearnsToCode/leetcode-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UniqueEmailAddresses.java
27 lines (25 loc) · 987 Bytes
/
UniqueEmailAddresses.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.HashSet;
import java.util.Set;
public class UniqueEmailAddresses {
public int numUniqueEmails(String[] emails) {
Set<String> uniqueEmails = new HashSet<>();
for (String emailAddress : emails) {
uniqueEmails.add(getActualEmailAddress(emailAddress));
}
return uniqueEmails.size();
}
private static String getActualEmailAddress(String email) {
StringBuilder result = new StringBuilder();
for (int index = 0 ; index < email.length() ; index++) {
if (email.charAt(index) == '+') {
while (email.charAt(index) != '@') index++;
result.append(email.substring(index));
break;
} else if (email.charAt(index) == '@') {
result.append(email.substring(index));
break;
} else if (email.charAt(index) != '.') result.append(email.charAt(index));
}
return result.toString();
}
}