A website domain such as "discuss.example.com" consists of several subdomains. At the top level there is "com", at the next level "example.com", and at the lowest level "discuss.example.com". A visit to "discuss.example.com" also counts as a visit to the parent domains "example.com" and "com".
A count-paired domain is a string in one of the two formats "rep d1.d2.d3" or "rep d1.d2", where rep is the number of visits to the domain and d1.d2.d3 is the domain itself. For example, "9001 discuss.example.com" says that discuss.example.com was visited 9001 times.
Given an array of count-paired domains cpdomains, return an array with the count-paired domain of every subdomain that appears in the input, where each count is the total number of visits to that subdomain.
Function Signature
def subdomain_visits(cpdomains: list[str]) -> list[str]:
Rules
-
Each input string is a visit count, one space, then a domain made of two or three lowercase labels separated by dots.
-
A visit to a domain also counts once for each of its parent domains, so every input adds its count to the full domain and to every suffix that starts right after a dot.
-
Each output string is the total count, one space, then the subdomain. Each subdomain appears exactly once in the output.
-
Return the output strings sorted in ascending order, compared as plain strings. Under this order
"5 org"
comes before
"50 yahoo.com"
(a space sorts before a digit), and
"10 a.com"
would come before
"9 b.com"
.
Constraints
-
1 <= len(cpdomains) <= 100
-
1 <= len(cpdomains[i]) <= 100
-
cpdomains[i]
follows either the
"rep d1.d2.d3"
format or the
"rep d1.d2"
format.
-
rep
is an integer in the range
[1, 10^4]
.
-
d1
,
d2
and
d3
consist of lowercase English letters.
Examples
Input: cpdomains = ["9001 discuss.example.com"]
Output: ["9001 com", "9001 discuss.example.com", "9001 example.com"]
There is only one domain, so "discuss.example.com", "example.com" and "com" are each visited 9001 times. In plain string order "9001 com" comes first.
Input: cpdomains = ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
Output: ["1 intel.mail.com", "5 org", "5 wiki.org", "50 yahoo.com", "900 google.mail.com", "901 mail.com", "951 com"]
"mail.com" collects 900 + 1 = 901 visits from its two children, "com" collects 900 + 50 + 1 = 951, and "org" gets the 5 visits of "wiki.org". The seven strings are listed in plain string order.