{"id":2406,"date":"2018-04-01T23:55:43","date_gmt":"2018-04-02T06:55:43","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2406"},"modified":"2018-04-01T23:59:06","modified_gmt":"2018-04-02T06:59:06","slug":"leetcode-811-subdomain-visit-count","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-811-subdomain-visit-count\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 811. Subdomain Visit Count"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>A website domain like &#8220;discuss.leetcode.com&#8221; consists of various subdomains. At the top level, we have &#8220;com&#8221;, at the next level, we have &#8220;leetcode.com&#8221;, and at the lowest level, &#8220;discuss.leetcode.com&#8221;. When we visit a domain like &#8220;discuss.leetcode.com&#8221;, we will also visit the parent domains &#8220;leetcode.com&#8221; and &#8220;com&#8221; implicitly.<\/p>\n<p>Now, call a &#8220;count-paired domain&#8221; to be a count (representing the number of visits this domain received), followed by a space, followed by the address. An example of a count-paired domain might be &#8220;9001 discuss.leetcode.com&#8221;.<\/p>\n<p>We are given a list\u00a0<code>cpdomains<\/code>\u00a0of count-paired domains. We would like a list of count-paired domains, (in the same format as the input, and in any order), that explicitly counts the number of visits to each subdomain.<\/p>\n<pre class=\"crayon:false\"><strong>Example 1:<\/strong>\r\n<strong>Input:<\/strong> \r\n[\"9001 discuss.leetcode.com\"]\r\n<strong>Output:<\/strong> \r\n[\"9001 discuss.leetcode.com\", \"9001 leetcode.com\", \"9001 com\"]\r\n<strong>Explanation:<\/strong> \r\nWe only have one website domain: \"discuss.leetcode.com\". As discussed above, the subdomain \"leetcode.com\" and \"com\" will also be visited. So they will all be visited 9001 times.\r\n\r\n<\/pre>\n<pre class=\"crayon:false \"><strong>Example 2:<\/strong>\r\n<strong>Input:<\/strong> \r\n[\"900 google.mail.com\", \"50 yahoo.com\", \"1 intel.mail.com\", \"5 wiki.org\"]\r\n<strong>Output:<\/strong> \r\n[\"901 mail.com\",\"50 yahoo.com\",\"900 google.mail.com\",\"5 wiki.org\",\"5 org\",\"1 intel.mail.com\",\"951 com\"]\r\n<strong>Explanation:<\/strong> \r\nWe will visit \"google.mail.com\" 900 times, \"yahoo.com\" 50 times, \"intel.mail.com\" once and \"wiki.org\" 5 times. For the subdomains, we will visit \"mail.com\" 900 + 1 = 901 times, \"com\" 900 + 50 + 1 = 951 times, and \"org\" 5 times.\r\n\r\n<\/pre>\n<p><strong>Notes:<\/strong><\/p>\n<ul>\n<li>The length of\u00a0<code>cpdomains<\/code>\u00a0will not exceed\u00a0<code>100<\/code>.<\/li>\n<li>The length of each domain name will not exceed\u00a0<code>100<\/code>.<\/li>\n<li>Each address will have either 1 or 2 &#8220;.&#8221; characters.<\/li>\n<li>The input count\u00a0in any count-paired domain will not exceed\u00a0<code>10000<\/code>.<\/li>\n<\/ul>\n<h1><strong>Solution: HashTable<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 16 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;string&gt; subdomainVisits(vector&lt;string&gt;&amp; cpdomains) {\r\n    unordered_map&lt;string, int&gt; counts;\r\n    for (const string&amp; cpdomain : cpdomains) {\r\n      size_t index = cpdomain.find(' ');\r\n      int count = std::stoi(cpdomain.substr(0, index));\r\n      string domain = cpdomain.substr(index + 1);\r\n      while (true) {\r\n        counts[domain] += count;\r\n        size_t i = domain.find('.');\r\n        if (i == string::npos) break;\r\n        domain = domain.substr(i + 1);\r\n      }\r\n    }\r\n    vector&lt;string&gt; ans;\r\n    for (const auto&amp; kv : counts)\r\n      ans.push_back(std::to_string(kv.second) + \" \" + kv.first);\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem A website domain like &#8220;discuss.leetcode.com&#8221; consists of various subdomains. At the top level, we have &#8220;com&#8221;, at the next level, we have &#8220;leetcode.com&#8221;, and&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70,47],"tags":[222,82,4],"class_list":["post-2406","post","type-post","status-publish","format-standard","hentry","category-hashtable","category-string","tag-easy","tag-hashtable","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=2406"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2406\/revisions"}],"predecessor-version":[{"id":2409,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2406\/revisions\/2409"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}