158. Total Scores of Leaf Domains

Asked in

Total Scores of Leaf Domains
You are given a list of domain names and an integer score for each of them.

A domain is a leaf if it does not have any child domains in the input.

A leaf domain's total score is the sum of:
  • its own score, and
  • the scores of all of its ancestor domains that are present in the input.
Write a program that, given the input list, returns all leaf domains with their respective total scores.

For example, for mail.test.mydomain.com, the ancestor domains are:
  • test.mydomain.com
  • mydomain.com
  • com

Method Signature

List<String> getLeafDomainsWithTotalScores(List<String> domainScores)
  • domainScores[i] is a string in the format "domain,score"
  • domain is a dot-separated domain name such as mail.test.com
  • score is an integer and may be negative, zero, or positive
  • Return each answer string in the format "leafDomain,totalScore"
  • Return the answer strings sorted in lexicographical ascending order by leaf domain.

Definitions

A domain a.b.c is a child of domain b.c.

More generally, a domain x is a child of domain y if:
  • x ends with "." + y
  • x has at least one additional label before y
A domain is a leaf if no input domain is its child.

The total score of a leaf domain is the sum of the scores of all domains on its path from itself up to the top-most ancestor present in the input.

Input Format

The input is provided as a list of strings.

Each string is in the format:

"domain,score"

Example entries:
  • "test.mydomain.com,10"
  • "com,20"
  • "www.test.com,-5"

Output Format

Return a list of strings.

Each string must be in the format:

"leafDomain,totalScore"

Example output entry:
  • "www.test.com,5"

Constraints

  • 1 ≤ domainScores.size() ≤ 100,000
  • Each domain name contains one or more non-empty labels separated by .
  • Each input string contains exactly one domain and one integer score separated by a comma
  • -10^6 ≤ score ≤ 10^6
  • All domain names in the input are unique
  • You must never use null as a parameter value

Notes

If a parent or ancestor domain is not present in the input, it does not contribute to the total score.

A domain with no child domains in the input is still a leaf even if it has one or more ancestors in the input.

Examples

Example 1

getLeafDomainsWithTotalScores( domainScores = List.of( "test.mydomain.com,10", "mail.test.mydomain.com,15", "test.com,-10", "com,20", "mydomain.com,5", "www.mydomain.com,10", "mail.test.com,10", "www.test.com,-5", "xyz.mail.test.com,25" ) )

Returns:

List.of( "www.test.com,5", "mail.test.mydomain.com,50", "www.mydomain.com,35", "xyz.mail.test.com,45" )

Explanation:
  • www.test.com = com + test.com + www.test.com = 20 + (-10) + (-5) = 5
  • mail.test.mydomain.com = com + mydomain.com + test.mydomain.com + mail.test.mydomain.com = 20 + 5 + 10 + 15 = 50
  • www.mydomain.com = com + mydomain.com + www.mydomain.com = 20 + 5 + 10 = 35
  • xyz.mail.test.com = com + test.com + mail.test.com + xyz.mail.test.com = 20 + (-10) + 10 + 25 = 45

mail.test.com is not returned because xyz.mail.test.com is its child domain in the input.

Example 2

getLeafDomainsWithTotalScores( domainScores = List.of( "com,3", "example.com,4", "mail.example.com,8", "shop.example.com,2" ) )

Returns:

List.of( "mail.example.com,15", "shop.example.com,9" )

Explanation:
  • mail.example.com = com + example.com + mail.example.com = 3 + 4 + 8 = 15
  • shop.example.com = com + example.com + shop.example.com = 3 + 4 + 2 = 9

Example 3

getLeafDomainsWithTotalScores( domainScores = List.of( "org,7" ) )

Returns:

List.of( "org,7" )

Explanation:
  • org has no child domains in the input, so it is a leaf
  • org also has no ancestor in the input, so its total score is its own score




Please use Laptop/Desktop or any other large screen to add/edit code.