-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
27 lines (24 loc) · 902 Bytes
/
Copy pathSolution.java
File metadata and controls
27 lines (24 loc) · 902 Bytes
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
class Solution
{
public List<String> subdomainVisits(String[] cpdomains)
{
Map<String, Integer> counts = new HashMap<>();
for (String cpdomain : cpdomains)
{
String[] line = cpdomain.split(" ");
int count = Integer.valueOf(line[0]);
String[] domains = line[1].split("\\.");
String parentDomain = "";
for (int i = domains.length - 1; i >= 0; i--)
{
parentDomain = domains[i] + (parentDomain.equals("") ? parentDomain : "." + parentDomain);
counts.put(parentDomain, counts.getOrDefault(parentDomain, 0) + count);
}
}
return counts.entrySet().stream().map(e -> e.getValue() + " " + e.getKey()).collect(Collectors.toList());
}
}