-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathSubdomainVisitCount811.java
72 lines (54 loc) · 1.69 KB
/
SubdomainVisitCount811.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package easy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*
Input:
["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
Output:
["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org",
"1 intel.mail.com","951 com"]
*/
public class SubdomainVisitCount811 {
public static List<String> subdomainVisits(String[] cpdomains) {
List<String> list = new ArrayList<String>();
HashMap<String, Integer> map = new HashMap<>();
for (String domain : cpdomains) {
int count = Integer.parseInt(domain.split(" ")[0]);
String full = domain.split(" ")[1];
map = insertMe(map, full, count);
String[] subdomain = domain.split("[.]", 2);
String sub1 = subdomain[1];
map = insertMe(map, sub1, count);
if (sub1.contains(".")) {
String[] ans2 = sub1.split("[.]");
map = insertMe(map, ans2[1], count);
}
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String key = entry.getKey();
int value = entry.getValue();
String ansStr = value + " " + key;
list.add(ansStr);
}
return list;
}
private static HashMap<String, Integer> insertMe(HashMap<String, Integer> map, String s, int count) {
if (!map.containsKey(s)) {
map.put(s, count);
} else {
map.put(s, map.get(s) + count);
}
return map;
}
public static void main(String[] args) {
String[] s = { "900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org" };
// String[] s = {"9001 discuss.leetcode.com"};
List<String> ans = subdomainVisits(s);
System.out.println(ans);
}
}
/* output:
[951 com, 900 google.mail.com, 1 intel.mail.com, 5 org, 5 wiki.org, 901 mail.com, 50 yahoo.com]
*/