Subdomain Visit Count

MediumArrays & Hashinghashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A domain such as "mail.example.com" is made of dot-separated labels. Visiting it also counts as a visit to each of its parent domains: "example.com" and "com". You are given cpdomains, where each entry has the form "<count> <domain>", meaning domain was visited count times. The same domain may appear in more than one entry. Return one string "<total> <domain>" for every domain or parent domain that received at least one visit, where total is its total visit count. The strings may be returned in **any order**.

Examples

Input: cpdomains = ["9001 discuss.leetcode.com"]
Output: ["9001 discuss.leetcode.com","9001 leetcode.com","9001 com"]
One domain and its two parent domains each receive 9001 visits.
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","5 wiki.org","5 org","1 intel.mail.com","951 com"]
`mail.com` collects `900 + 1`, and `com` collects `900 + 50 + 1`.

Constraints

  • `1 <= cpdomains.length <= 100`
  • `1 <= cpdomains[i].length <= 100`
  • Each entry is `"<count> <domain>"` with `1 <= count <= 10^4`
  • Each domain has 1 to 3 labels of lowercase English letters joined by `.`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems