-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgroupEmail.java
More file actions
48 lines (47 loc) · 1.77 KB
/
groupEmail.java
File metadata and controls
48 lines (47 loc) · 1.77 KB
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
42
43
44
45
46
47
48
import java.util.*;
/* Google oa
Here are a bunch of emails. You find some of them may be duplicate. Let's say an email address contains a local name
and a domain name. "ab@email.com" "ab" is the local name and "gmail.com" is the domain name.
local name: igore all dots '.' between characters & remove all characters after "+"
ex: "dup....licate+a@gmail.com" equals to "duplicate@email.com"
* */
public class groupEmail {
public static void main(String[] args){
groupEmail obj = new groupEmail();
String[] emails = {"ab@gmail.com", "a.....b@gmail.com", "a+b@abc.com", "..a...@abc.com"};
System.out.println(obj.group(emails));
}
// return the number of groups which contains more that 2 email addresses
private int group(String[] emails){
Map<String, Integer> map = new HashMap<>();
int counter = 0;
for(String email : emails){
// helper(email)
String pattern = extract(email);
//map.putIfAbsent(pattern, 0);
map.put(pattern, map.getOrDefault(pattern,0)+1);
if(map.get(pattern) == 2){
counter ++;
}
}
return counter;
}
private String extract(String email){
StringBuilder sb = new StringBuilder();
int index = email.indexOf('@');
String name = email.substring(0,index);
String domain = email.substring(index, email.length());
for(int i = 0; i < name.length(); i++){
if(name.charAt(i) == '.'){
continue;
}
else if(name.charAt(i) == '+'){
break;
}else{
sb.append(name.charAt(i));
}
}
System.out.println(sb.toString()+domain);
return sb.toString()+domain;
}
}