Community Problem

Subdomain Visit Count

Difficulty: Medium

Aggregate visit counts across every subdomain prefix from a list of count-paired domain strings.

Subdomain Visit Count

Aggregate visit counts across every subdomain prefix from a list of count-paired domain strings.

MEDIUM
Free
arrays
hash-table
strings
jamesmurphy

By @jamesmurphy

May 9, 2026

·

Updated May 18, 2026

561 views

16

4.4 (10)

Hit this one on a takehome screening round at an analytics startup, framed as a log aggregation warmup. The official catalog has Group Anagrams and Top K Frequent for the hash-table family but skipped the every-suffix-counts variation, which is the prettier teaching example for "build the key on the fly while iterating" and is borderline indistinguishable from real production traffic-rollup code.

Subdomain Visit Count

A website domain like discuss.leetcode.com consists of several subdomains. At the top level we have com, at the next level we have leetcode.com, and at the lowest level we have discuss.leetcode.com. When we visit discuss.leetcode.com, we are also implicitly visiting its parent domains leetcode.com and com.

A count-paired domain is a string of the form "<count> <domain>" where count is a positive integer representing the number of visits the domain received. For example, "9001 discuss.leetcode.com" means there were 9001 visits to discuss.leetcode.com.

Given an array cpdomains of count-paired domains, return an array of count-paired domains for each subdomain in the input, summed across all inputs. The output may be returned in any order.

Examples

Example 1:

  • Input: cpdomains = ["9001 discuss.leetcode.com"]
  • Output: ["9001 leetcode.com", "9001 discuss.leetcode.com", "9001 com"] (any order)
  • Explanation: Visiting discuss.leetcode.com once with weight 9001 implicitly visits leetcode.com and com with the same weight.

Example 2:

  • Input: cpdomains = ["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"]
  • Explanation: mail.com aggregates the 900 from google.mail.com and the 1 from intel.mail.com (total 901). com aggregates 900 + 50 + 1 = 951.

Example 3:

  • Input: cpdomains = ["5 a.b.c.d"]
  • Output: ["5 a.b.c.d", "5 b.c.d", "5 c.d", "5 d"]
  • Explanation: Every suffix starting at a domain boundary inherits the count.

Constraints

  • 1 <= cpdomains.length <= 100
  • 1 <= cpdomains[i].length <= 100
  • cpdomains[i] follows the format "<count> <domain>" where count is a positive integer with at most nine digits and domain consists of lowercase letters and dots.
  • The number of dots in a domain is between 0 and 3.

Follow-up

The count-paired strings can be parsed once into (count, domain) and then split on dots, but you can also keep moving the dot index forward and slice in place. Both are O(total characters) and the second avoids the intermediate split array. Either is fine for the given constraints.

Solution

Hints

0/4
Hint 1
Hint 2
Hint 3
Hint 4
All Problems