Subdomain Visit Counts
Problem statement
Each row is a decimal visit count followed by a comma and a domain, such as 60,mail.yahoo.com. Add its count to that domain and to every suffix beginning after a dot. Thus the example contributes sixty visits to mail.yahoo.com, yahoo.com and com.
Combine contributions from all rows, including repeated domains. Return one string total,domain for every observed domain suffix, sorted lexicographically by the domain. Totals use ordinary decimal notation. Retain observed suffixes even when their total is zero. An empty input returns an empty array.
Domains are hierarchical string identifiers under the grammar below; do not use public-suffix rules or network lookups. A one-label domain contributes to itself.
Function
subdomainCounts(rows: String[]) → String[]Examples
Example 1
rows = ["900,google.com","60,mail.yahoo.com","10,mobile.sports.yahoo.com","40,sports.yahoo.com","300,yahoo.com","10,stackoverflow.com","2,en.wikipedia.org","1,es.wikipedia.org","1,mobile.sports"]return = ["1320,com","2,en.wikipedia.org","1,es.wikipedia.org","900,google.com","60,mail.yahoo.com","1,mobile.sports","10,mobile.sports.yahoo.com","3,org","1,sports","50,sports.yahoo.com","10,stackoverflow.com","3,wikipedia.org","410,yahoo.com"]The four inputs under yahoo.com contribute 60+10+40+300=410 there. The two under sports.yahoo.com contribute 10+40=50. The com total is 900+410+10=1320. The separate mobile.sports row also creates the one-label suffix sports.
Example 2
rows = ["5,a.b","7,b"]return = ["5,a.b","12,b"]The first row contributes five to a.b and b. The second adds seven more to b, giving twelve. Sorting by domain puts a.b first.
Example 3
rows = []return = []No suffix is observed, so the result is empty.
Constraints
0 <= rows.length <= 100- Every row has between
3and71characters and exactly the formcount,domain, with no spaces. - A count is between
0and1000000000, written as0or without a leading zero. - A domain has between
1and5dot-separated labels and at most60characters. Each label has1through20lowercase ASCII letters, digits or hyphens, and begins and ends with a letter or digit. - The sum of the domain lengths across all rows is at most
1000.