{"id":7786,"date":"2020-12-12T16:19:17","date_gmt":"2020-12-13T00:19:17","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7786"},"modified":"2020-12-12T16:25:03","modified_gmt":"2020-12-13T00:25:03","slug":"leetcode-1684-count-the-number-of-consistent-strings","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1684-count-the-number-of-consistent-strings\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1684. Count the Number of Consistent Strings"},"content":{"rendered":"\n<p>You are given a string&nbsp;<code>allowed<\/code>&nbsp;consisting of&nbsp;<strong>distinct<\/strong>&nbsp;characters and an array of strings&nbsp;<code>words<\/code>. A string is&nbsp;<strong>consistent&nbsp;<\/strong>if all characters in the string appear in the string&nbsp;<code>allowed<\/code>.<\/p>\n\n\n\n<p>Return<em>&nbsp;the number of&nbsp;<strong>consistent<\/strong>&nbsp;strings in the array&nbsp;<\/em><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> allowed = \"ab\", words = [\"ad\",\"bd\",\"aaab\",\"baa\",\"badab\"]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> Strings \"aaab\" and \"baa\" are consistent since they only contain characters 'a' and 'b'.\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> allowed = \"abc\", words = [\"a\",\"b\",\"c\",\"ab\",\"ac\",\"bc\",\"abc\"]\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> All strings are consistent.\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> allowed = \"cad\", words = [\"cc\",\"acd\",\"b\",\"ba\",\"bac\",\"bad\",\"ac\",\"d\"]\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> Strings \"cc\", \"acd\", \"ac\", and \"d\" are consistent.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= words.length &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>1 &lt;= allowed.length &lt;=26<\/code><\/li><li><code>1 &lt;= words[i].length &lt;= 10<\/code><\/li><li>The characters in&nbsp;<code>allowed<\/code>&nbsp;are&nbsp;<strong>distinct<\/strong>.<\/li><li><code>words[i]<\/code>&nbsp;and&nbsp;<code>allowed<\/code>&nbsp;contain only 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(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\nclass Solution {\npublic:\n  int countConsistentStrings(string allowed, vector<string>& words) {\n    vector<int> m(26);\n    for (char c : allowed) m[c - 'a'] = 1;\n    int ans = 0;\n    for (const string& w : words)      \n      ans += all_of(begin(w), end(w), [&m](char c) { return m[c - 'a']; });\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def countConsistentStrings(self, allowed: str, words: List[str]) -> int:    \n    return sum(all(c in allowed for c in w) for w in words)\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string&nbsp;allowed&nbsp;consisting of&nbsp;distinct&nbsp;characters and an array of strings&nbsp;words. A string is&nbsp;consistent&nbsp;if all characters in the string appear in the string&nbsp;allowed. Return&nbsp;the number&#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-7786","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\/7786","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=7786"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7786\/revisions"}],"predecessor-version":[{"id":7788,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7786\/revisions\/7788"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}