{"id":4922,"date":"2019-03-02T20:22:57","date_gmt":"2019-03-03T04:22:57","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4922"},"modified":"2019-03-02T20:24:03","modified_gmt":"2019-03-03T04:24:03","slug":"leetcode-1002-find-common-characters","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1002-find-common-characters\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1002. Find Common Characters"},"content":{"rendered":"\n<p>Given an array&nbsp;<code>A<\/code>&nbsp;of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list&nbsp;<strong>(including duplicates)<\/strong>.&nbsp;&nbsp;For example, if a character occurs 3 times&nbsp;in all strings but not 4 times, you need to include that character three times&nbsp;in the final answer.<\/p>\n\n\n\n<p>You may return the answer in any order.<\/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>[\"bella\",\"label\",\"roller\"]\n<strong>Output: <\/strong>[\"e\",\"l\",\"l\"]\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>[\"cool\",\"lock\",\"cook\"]\n<strong>Output: <\/strong>[\"c\",\"o\"]\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= A.length &lt;= 100<\/code><\/li><li><code>1 &lt;= A[i].length &lt;= 100<\/code><\/li><li><code>A[i][j]<\/code>&nbsp;is a lowercase letter<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Min count for each character<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n*l)<br>Space complexity: O(1)<\/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, running time: 12 ms\nclass Solution {\npublic:\n  vector<string> commonChars(vector<string>& A) {\n    vector<int> min_count(26, INT_MAX);\n    for (const string& a : A) {\n      vector<int> count(26, 0);\n      for (const char ch : a)\n        ++count[ch - 'a'];\n      for (int i = 0; i < 26; ++i)\n        min_count[i] = min(min_count[i], count[i]);\n    }\n    vector<string> ans;\n    for (int i = 0; i < 26; ++i) {\n      if (min_count[i] == INT_MAX) continue;\n      for (int j = 0; j < min_count[i]; ++j)\n        ans.push_back(string(1, 'a' + i));\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array&nbsp;A&nbsp;of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list&nbsp;(including duplicates).&nbsp;&nbsp;For&#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":[82],"class_list":["post-4922","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4922","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=4922"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4922\/revisions"}],"predecessor-version":[{"id":4926,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4922\/revisions\/4926"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}