{"id":2487,"date":"2018-04-14T20:23:42","date_gmt":"2018-04-15T03:23:42","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2487"},"modified":"2018-04-14T20:24:13","modified_gmt":"2018-04-15T03:24:13","slug":"leetcode-819-most-common-word","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-819-most-common-word\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 819. Most Common Word"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u627e\u51fa\u4e00\u4e2a\u6bb5\u843d\u4e2d\u51fa\u73b0\u6b21\u6570\u6700\u591a\u7684\u5355\u8bcd\u3002<\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/most-common-word\/description\/\">https:\/\/leetcode.com\/problems\/most-common-word\/description\/<\/a><\/p>\n<p>Given a paragraph\u00a0and a list of banned words, return the most frequent word that is not in the list of banned words.\u00a0 It is guaranteed there is at least one word that isn&#8217;t banned, and that the answer is unique.<\/p>\n<p>Words in the list of banned words are given in lowercase, and free of punctuation.\u00a0 Words in the paragraph are not case sensitive.\u00a0 The answer is in lowercase.<\/p>\n<pre class=\"crayon:false \"><strong>Example:<\/strong>\r\n<strong>Input:<\/strong> \r\nparagraph = \"Bob hit a ball, the hit BALL flew far after it was hit.\"\r\nbanned = [\"hit\"]\r\n<strong>Output:<\/strong> \"ball\"\r\n<strong>Explanation:<\/strong> \r\n\"hit\" occurs 3 times, but it is a banned word.\r\n\"ball\" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph. \r\nNote that words in the paragraph are not case sensitive,\r\nthat punctuation is ignored (even if adjacent to words, such as \"ball,\"), \r\nand that \"hit\" isn't the answer even though it occurs more because it is banned.\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= paragraph.length &lt;= 1000<\/code>.<\/li>\n<li><code>1 &lt;= banned.length &lt;= 100<\/code>.<\/li>\n<li><code>1 &lt;= banned[i].length &lt;= 10<\/code>.<\/li>\n<li>The answer is unique, and written in lowercase (even if its occurrences in\u00a0<code>paragraph<\/code>\u00a0may have\u00a0uppercase symbols, and even if it is a proper noun.)<\/li>\n<li><code>paragraph<\/code>\u00a0only consists of letters, spaces, or the punctuation symbols\u00a0<code>!?',;.<\/code><\/li>\n<li>Different words in\u00a0<code>paragraph<\/code>\u00a0are always separated by a space.<\/li>\n<li>There are no hyphens or hyphenated words.<\/li>\n<li>Words only consist of letters, never apostrophes or other punctuation symbols.<\/li>\n<\/ul>\n<h1><strong>Solution: Hashtable<\/strong><\/h1>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 8 ms\r\nclass Solution {\r\npublic:\r\n  string mostCommonWord(string paragraph, vector&lt;string&gt;&amp; banned) {\r\n    unordered_set&lt;string&gt; b(banned.begin(), banned.end());\r\n    unordered_map&lt;string, int&gt; counts;\r\n    const string pattern = \"!?',;. \";\r\n    int best = 0;\r\n    string ans;\r\n    const int n = paragraph.size();\r\n    string word;\r\n    for (int i = 0; i &lt;= n; ++i) {\r\n      if (i == n || pattern.find(paragraph[i]) != string::npos) {\r\n        if (++counts[word] &gt; best &amp;&amp; !b.count(word) &amp;&amp; word.size() &gt; 0) {\r\n          best = counts[word];\r\n          ans = word;\r\n        } \r\n        word.clear();\r\n      } else {\r\n        word += tolower(paragraph[i]);\r\n      }\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u627e\u51fa\u4e00\u4e2a\u6bb5\u843d\u4e2d\u51fa\u73b0\u6b21\u6570\u6700\u591a\u7684\u5355\u8bcd\u3002 https:\/\/leetcode.com\/problems\/most-common-word\/description\/ Given a paragraph\u00a0and a list of banned words, return the most frequent word that is not in the list of banned words.\u00a0&#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":[82,177,4],"class_list":["post-2487","post","type-post","status-publish","format-standard","hentry","category-hashtable","category-string","tag-hashtable","tag-medium","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2487","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=2487"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2487\/revisions"}],"predecessor-version":[{"id":2489,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2487\/revisions\/2489"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}