{"id":5446,"date":"2019-08-17T23:07:06","date_gmt":"2019-08-18T06:07:06","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5446"},"modified":"2019-08-18T12:29:03","modified_gmt":"2019-08-18T19:29:03","slug":"leetcode-1160-find-words-that-can-be-formed-by-characters","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1160-find-words-that-can-be-formed-by-characters\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1160. Find Words That Can Be Formed by Characters"},"content":{"rendered":"\n<p>You are given an array of strings&nbsp;<code>words<\/code>&nbsp;and a string&nbsp;<code>chars<\/code>.<\/p>\n\n\n\n<p>A string is&nbsp;<em>good<\/em>&nbsp;if&nbsp;it can be formed by&nbsp;characters from&nbsp;<code>chars<\/code>&nbsp;(each character&nbsp;can only be used once).<\/p>\n\n\n\n<p>Return the sum of lengths of all good strings in&nbsp;<code>words<\/code>.<\/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>words = [\"cat\",\"bt\",\"hat\",\"tree\"], chars = \"atach\"\n<strong>Output: <\/strong>6\n<strong>Explanation: <\/strong>\nThe strings that can be formed are \"cat\" and \"hat\" so the answer is 3 + 3 = 6.\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>words = [\"hello\",\"world\",\"leetcode\"], chars = \"welldonehoneyr\"\n<strong>Output: <\/strong>10\n<strong>Explanation: <\/strong>\nThe strings that can be formed are \"hello\" and \"world\" so the answer is 5 + 5 = 10.\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= words.length &lt;= 1000<\/code><\/li><li><code>1 &lt;= words[i].length, chars.length&nbsp;&lt;= 100<\/code><\/li><li>All strings contain lowercase English letters only.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Use a hashtable to store each letter&#8217;s frequency of the string and compare that with each word.<\/p>\n\n\n\n<p>Time complexity: O(n + sum(len(word))<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, 72 ms, 20 MB\nclass Solution {\npublic:\n  int countCharacters(vector<string>& words, string chars) {\n    vector<int> counts(26);\n    for (char c : chars) ++counts[c - 'a'];\n    int ans = 0;\n    for (const string& word : words) {\n      vector<int> cur(26);\n      bool valid = true;\n      for (char c: word)\n        if (++cur[c - 'a'] > counts[c - 'a']) {\n          valid = false;\n          break;\n        }\n      if (valid) ans += word.length();\n    }\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python#<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def countCharacters(self, words: List[str], chars: str) -> int:    \n    counts = collections.Counter(chars)\n    def isValid(word):\n      cur = collections.Counter(word)\n      for c in cur:\n        if c not in counts or cur[c] > counts[c]:\n          return False\n      return True\n    \n    ans = 0\n    for word in words:\n      ans += len(word) if isValid(word) else 0\n    return ans\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array of strings&nbsp;words&nbsp;and a string&nbsp;chars. A string is&nbsp;good&nbsp;if&nbsp;it can be formed by&nbsp;characters from&nbsp;chars&nbsp;(each character&nbsp;can only be used once). Return the sum&#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-5446","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\/5446","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=5446"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5446\/revisions"}],"predecessor-version":[{"id":5454,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5446\/revisions\/5454"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}