{"id":3496,"date":"2018-08-12T01:30:45","date_gmt":"2018-08-12T08:30:45","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3496"},"modified":"2018-08-12T01:31:32","modified_gmt":"2018-08-12T08:31:32","slug":"leetcode-888-uncommon-words-from-two-sentences","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-888-uncommon-words-from-two-sentences\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 888. Uncommon Words from Two Sentences"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>We are given two sentences\u00a0<code>A<\/code>\u00a0and\u00a0<code>B<\/code>.\u00a0 (A\u00a0<em>sentence<\/em>\u00a0is a string of space separated words.\u00a0 Each\u00a0<em>word<\/em>\u00a0consists only of lowercase letters.)<\/p>\n<p>A word is\u00a0<em>uncommon<\/em>\u00a0if it appears exactly once in one of the sentences, and does not appear in the other sentence.<\/p>\n<p>Return a list of all uncommon words.<\/p>\n<p>You may return the list in any order.<\/p>\n<div>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>A = <span id=\"example-input-1-1\">\"this apple is sweet\"<\/span>, B = <span id=\"example-input-1-2\">\"this apple is sour\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">[\"sweet\",\"sour\"]<\/span>\r\n<\/pre>\n<div>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false \"><strong>Input: <\/strong>A = <span id=\"example-input-2-1\">\"apple apple\"<\/span>, B = <span id=\"example-input-2-2\">\"banana\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">[\"banana\"]<\/span>\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>0 &lt;= A.length &lt;= 200<\/code><\/li>\n<li><code>0 &lt;= B.length &lt;= 200<\/code><\/li>\n<li><code>A<\/code>\u00a0and\u00a0<code>B<\/code>\u00a0both contain only spaces and lowercase letters.<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<h1><strong>Solution: HashTable<\/strong><\/h1>\n<p>Time complexity: O(m+n)<\/p>\n<p>Space complexity: O(m+n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">class Solution {\r\npublic:\r\n  vector&lt;string&gt; uncommonFromSentences(string A, string B) {\r\n    istringstream ss(A + \" \" + B);\r\n    unordered_map&lt;string, int&gt; counts;\r\n    string s;\r\n    while (ss &gt;&gt; s) ++counts[s];\r\n    vector&lt;string&gt; ans;\r\n    for (const auto&amp; p : counts)\r\n      if (p.second == 1)\r\n        ans.push_back(p.first);\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem We are given two sentences\u00a0A\u00a0and\u00a0B.\u00a0 (A\u00a0sentence\u00a0is a string of space separated words.\u00a0 Each\u00a0word\u00a0consists only of lowercase letters.) A word is\u00a0uncommon\u00a0if it appears exactly once&#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],"tags":[222,82,4],"class_list":["post-3496","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-easy","tag-hashtable","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3496","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=3496"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3496\/revisions"}],"predecessor-version":[{"id":3498,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3496\/revisions\/3498"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}