forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubdomain Visit Count.java
29 lines (23 loc) · 1.02 KB
/
Subdomain Visit Count.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
class Solution {
public List<String> subdomainVisits(String[] cpdomains) {
List<String> result = new LinkedList<>();
HashMap<String, Integer> hmap = new HashMap<>();
for(int i = 0; i < cpdomains.length; i++){
String[] stringData = cpdomains[i].split(" ");
String[] str = stringData[1].split("\\.");
String subDomains = "";
for(int j = str.length-1; j >= 0; j--){
subDomains = str[j] + subDomains;
if(!hmap.containsKey(subDomains))
hmap.put(subDomains, Integer.parseInt(stringData[0]));
else
hmap.put(subDomains, hmap.get(subDomains) + Integer.parseInt(stringData[0]));
subDomains = "." + subDomains;
}
}
for(Map.Entry<String, Integer> entry: hmap.entrySet()){
result.add(entry.getValue() + " " + entry.getKey());
}
return result;
}
}