forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUniqueEmailAddresses.java
41 lines (36 loc) · 1.08 KB
/
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Solution {
// O(Len(emails)* max(Len(Email))
public int numUniqueEmails(String[] emails) {
Set<String> sanitizedEmails = new HashSet<>();
for(String email : emails){
String updatedEmail = getSanitizeEmail(email);
sanitizedEmails.add(updatedEmail);
}
return sanitizedEmails.size();
}
private String getSanitizeEmail(String email){
String localName ="";
int i = 0;
while(i<email.length() && email.charAt(i) !='@'){
if(email.charAt(i) == '.'){
i++;
} else if(email.charAt(i) == '+'){
while(i<email.length() && email.charAt(i) !='@'){
i++;
}
} else {
localName += email.charAt(i);
i++;
}
}
// skip one char for @
i++;
String domain = "";
while(i<email.length()){
System.out.println(i);
domain +=email.charAt(i);
i++;
}
return localName + "@" + domain;
}
}