{"id":8811,"date":"2021-11-27T08:30:22","date_gmt":"2021-11-27T16:30:22","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8811"},"modified":"2021-11-27T08:30:59","modified_gmt":"2021-11-27T16:30:59","slug":"leetcode-2085-count-common-words-with-one-occurrence","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2085-count-common-words-with-one-occurrence\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2085. Count Common Words With One Occurrence"},"content":{"rendered":"\n<p>Given two string arrays&nbsp;<code>words1<\/code>&nbsp;and&nbsp;<code>words2<\/code>, return&nbsp;<em>the number of strings that appear&nbsp;<strong>exactly once<\/strong>&nbsp;in&nbsp;<strong>each<\/strong>&nbsp;of the two arrays.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> words1 = [\"leetcode\",\"is\",\"amazing\",\"as\",\"is\"], words2 = [\"amazing\",\"leetcode\",\"is\"]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong>\n- \"leetcode\" appears exactly once in each of the two arrays. We count this string.\n- \"amazing\" appears exactly once in each of the two arrays. We count this string.\n- \"is\" appears in each of the two arrays, but there are 2 occurrences of it in words1. We do not count this string.\n- \"as\" appears once in words1, but does not appear in words2. We do not count this string.\nThus, there are 2 strings that appear exactly once in each of the two arrays.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> words1 = [\"b\",\"bb\",\"bbb\"], words2 = [\"a\",\"aa\",\"aaa\"]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> There are no strings that appear in each of the two arrays.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> words1 = [\"a\",\"ab\"], words2 = [\"a\",\"a\",\"a\",\"ab\"]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> The only string that appears exactly once in each of the two arrays is \"ab\".\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= words1.length, words2.length &lt;= 1000<\/code><\/li><li><code>1 &lt;= words1[i].length, words2[j].length &lt;= 30<\/code><\/li><li><code>words1[i]<\/code>&nbsp;and&nbsp;<code>words2[j]<\/code>&nbsp;consists only of lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n + m)<br>Space complexity: O(n + m)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int countWords(vector<string>& words1, vector<string>& words2) {\n    unordered_map<string, int> c1;\n    unordered_map<string, int> c2;\n    for (const string& w : words1) ++c1[w];\n    for (const string& w : words2) ++c2[w];\n    int ans = 0;\n    for (const auto& [w, c] : c1)\n      if (c == 1 && c2[w] == 1) ++ans;\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given two string arrays&nbsp;words1&nbsp;and&nbsp;words2, return&nbsp;the number of strings that appear&nbsp;exactly once&nbsp;in&nbsp;each&nbsp;of the two arrays. Example 1: Input: words1 = [&#8220;leetcode&#8221;,&#8221;is&#8221;,&#8221;amazing&#8221;,&#8221;as&#8221;,&#8221;is&#8221;], words2 = [&#8220;amazing&#8221;,&#8221;leetcode&#8221;,&#8221;is&#8221;] Output: 2&#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],"class_list":["post-8811","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-easy","tag-hashtable","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8811","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=8811"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8811\/revisions"}],"predecessor-version":[{"id":8813,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8811\/revisions\/8813"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}