326. Subdomain Visit Count

Medium
Hierarchical Frequency Aggregation
Hashing

Problem

A count-paired domain contains a visit count followed by a domain. A visit to a domain also counts as a visit to every parent subdomain. Given an array cpdomains, return the accumulated visit count for every visited domain and subdomain. The result may be returned in any order.

Examples

Example 1

Input: ["9001 discuss.leetcode.com"]
Output: ["9001 discuss.leetcode.com","9001 leetcode.com","9001 com"]

Example 2

Input: ["900 google.mail.com","50 yahoo.com","1 intel.mail.com","5 wiki.org"]
Output: ["900 google.mail.com","901 mail.com","951 com","50 yahoo.com","1 intel.mail.com","5 wiki.org","5 org"]

Example 3

Input: ["10 a.com","20 b.com"]
Output: ["10 a.com","20 b.com","30 com"]
Constraints

1 <= cpdomains.length <= 100

Each entry contains a positive visit count followed by one domain.

Domains contain lowercase English letters and dots.

Hints

?? Split each entry into its count and full domain.

?? Generate the full domain and every suffix after a dot.

?? Accumulate all counts in one hash map.

Expected Complexity
Time: O(total input characters)
Space: O(number of distinct subdomains)
Follow-up

Can you generate parent subdomains without repeatedly splitting the same string?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
Input
["9001 discuss.leetcode.com"]
Expected
["9001 discuss.leetcode.com","9001 leetcode.com","9001 com"]