Subdomain Visit Count

A domain like `"discuss.leetcode.com"` is made of labels separated by dots. Visiting it also counts as a visit to every parent domain: `"leetcode.com"` and `"com"`. A **count-paired domain** is a string `"rep d"`, meaning domain `d` was visited `rep` times, for example `"9001 discuss.leetcode.com"`. Given an array of count-paired domains `cpdomains`, return *an array of the count-paired domains of every subdomain that was visited*, in **any order**.

30:00

Subdomain Visit Count
medium
Free written solution
Topics
Companies

A domain like "discuss.leetcode.com" is made of labels separated by dots. Visiting it also counts as a visit to every parent domain: "leetcode.com" and "com".

A count-paired domain is a string "rep d", meaning domain d was visited rep times, for example "9001 discuss.leetcode.com".

Given an array of count-paired domains cpdomains, return an array of the count-paired domains of every subdomain that was visited, in any order.

Example 1:
Input: {"cpdomains":["9001 discuss.leetcode.com"]}
Output: ["9001 discuss.leetcode.com","9001 leetcode.com","9001 com"]
Constraints:
  • 1≤cpdomains.length≤1001 \leq \text{cpdomains.length} \leq 100

  • 1≤cpdomains[i].length≤1001 \leq \text{cpdomains}[i].\text{length} \leq 100

  • cpdomains[i] has the form "rep d1.d2.d3" or "rep d1.d2"

  • 1≤rep≤1041 \leq \text{rep} \leq 10^4

  • d1, d2 and d3 are lowercase English letters

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"cpdomains":["9001 discuss.leetcode.com"]}

Every visit to a domain is also a visit to each parent domain, so add the count to every suffix of the domain in one hash map.

input

9001 discuss.leetcode.com

peel labels off the left

Waiting for the first entry.

counts map, drawn as a domain tree (0 keys)

empty

visit count of the entry
suffix being credited
map key just updated
Variables
VariableValue
keys0
DepthFunction Call
Stack empty
0/5