forked from qiyuangong/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path811_Subdomain_Visit_Count.java
More file actions
22 lines (21 loc) · 931 Bytes
/
811_Subdomain_Visit_Count.java
File metadata and controls
22 lines (21 loc) · 931 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public List<String> subdomainVisits(String[] cpdomains) {
// https://leetcode.com/problems/subdomain-visit-count/discuss/121738/C%2B%2BJavaPython-Easy-Understood-Solution
Map<String, Integer> map = new HashMap();
for (String cpdomain : cpdomains) {
int i = cpdomain.indexOf(' ');
int n = Integer.valueOf(cpdomain.substring(0, i));
String domain = cpdomain.substring(i + 1);
for (i = 0; i < domain.length(); ++i) {
if (domain.charAt(i) == '.') {
String d = domain.substring(i + 1);
map.put(d, map.getOrDefault(d, 0) + n);
}
}
map.put(domain, map.getOrDefault(domain, 0) + n);
}
List<String> res = new ArrayList();
for (String domain : map.keySet()) res.add(map.get(domain) + " " + domain);
return res;
}
}