# Code language: Python classSolution: defsubdomainVisits(self, cpdomains: List[str]) -> List[str]: cnt = Counter() for cpd in cpdomains: tim, dom = cpd.split() tim = int(tim) dom = dom.split(".") for i inrange(len(dom)): cnt['.'.join(dom[i:])] += tim return ["{} {}".format(v, k) for k, v in cnt.items()]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Code language: Java classSolution { public List<String> subdomainVisits(String[] cpdomains) { Map<String, Integer> cnt = newHashMap<>(); for (String s:cpdomains) { String[] cpd = s.split(" "); inttim= Integer.parseInt(cpd[0]); String[] domain = cpd[1].split("\\."); Stringcur=""; for (inti= domain.length - 1; i >= 0; --i) { cur = domain[i] + (i == domain.length - 1 ? "" : ".") + cur; cnt.put(cur, tim + cnt.getOrDefault(cur, 0)); } } List<String> ans = newArrayList<>(); for (String k : cnt.keySet()) ans.add(cnt.get(k) + " " + k); return ans; } }