Subdomain Visit Count

Medium~20 min

A website domain like "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com", and at the lowest level, "discuss.leetcode.com".

When we visit a domain like "discuss.leetcode.com", we will also visit its parent domains "leetcode.com" and "com" implicitly.

A count-paired domain is a domain that has a visit count paired with it, e.g. "9001 discuss.leetcode.com".

Given an array of count-paired domains cpdomains, return an array of the count-paired domains of each subdomain. The output can be in any order.

Examples

Example 1
Input: cpdomains = ["9001 discuss.leetcode.com"]
Output: ["9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"]
Explanation: We only have one domain. "discuss.leetcode.com" was visited 9001 times, so "leetcode.com" and "com" were also each visited 9001 times.
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", "1 intel.mail.com", "5 wiki.org", "5 org", "951 com"]
Explanation: "mail.com" gets 900 + 1 = 901 visits. "com" gets 900 + 50 + 1 = 951 visits. Each full domain retains its own count.

Constraints

  • 1 <= cpdomains.length <= 100
  • 1 <= cpdomains[i].length <= 100
  • cpdomains[i] follows the format "count domain"
  • 1 <= count <= 10^4
  • Each domain has either 1 or 2 dots.
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run