{"id":5521,"date":"2019-09-01T18:28:10","date_gmt":"2019-09-02T01:28:10","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5521"},"modified":"2019-09-01T21:03:24","modified_gmt":"2019-09-02T04:03:24","slug":"leetcode-1178-number-of-valid-words-for-each-puzzle","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1178-number-of-valid-words-for-each-puzzle\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1178. Number of Valid Words for Each Puzzle"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1178. Number of Valid Words for Each Puzzle - \u5237\u9898\u627e\u5de5\u4f5c EP267\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/qOrmhPX-gAU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>\n\nWith respect to a given&nbsp;<code>puzzle<\/code>&nbsp;string, a&nbsp;<code>word<\/code>&nbsp;is&nbsp;<em>valid<\/em>&nbsp;if both the following conditions are satisfied:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>word<\/code>&nbsp;contains the first letter of&nbsp;<code>puzzle<\/code>.<\/li><li>For each letter in&nbsp;<code>word<\/code>, that letter is in&nbsp;<code>puzzle<\/code>.<br>For example, if the puzzle is &#8220;abcdefg&#8221;, then valid words are &#8220;faced&#8221;, &#8220;cabbage&#8221;, and &#8220;baggage&#8221;; while invalid words are &#8220;beefed&#8221; (doesn&#8217;t include &#8220;a&#8221;) and &#8220;based&#8221; (includes &#8220;s&#8221; which isn&#8217;t in the puzzle).<\/li><\/ul>\n\n\n\n<p>Return an array&nbsp;<code>answer<\/code>, where&nbsp;<code>answer[i]<\/code>&nbsp;is the number of words in the given word list&nbsp;<code>words<\/code>&nbsp;that are valid with respect to the puzzle&nbsp;<code>puzzles[i]<\/code>.<\/p>\n\n\n\n<p><strong>Example :<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> \nwords = [\"aaaa\",\"asas\",\"able\",\"ability\",\"actt\",\"actor\",\"access\"], \npuzzles = [\"aboveyz\",\"abrodyz\",\"abslute\",\"absoryz\",\"actresz\",\"gaswxyz\"]\n<strong>Output:<\/strong> [1,1,3,2,4,0]\n<strong>Explanation:<\/strong>\n1 valid word&nbsp;for \"aboveyz\" : \"aaaa\" \n1 valid word&nbsp;for \"abrodyz\" : \"aaaa\"\n3 valid words for \"abslute\" : \"aaaa\", \"asas\", \"able\"\n2 valid words for&nbsp;\"absoryz\" : \"aaaa\", \"asas\"\n4 valid words for&nbsp;\"actresz\" : \"aaaa\", \"asas\", \"actt\", \"access\"\nThere're&nbsp;no valid words for&nbsp;\"gaswxyz\" cause none of the words in the list contains letter 'g'.\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^5<\/code><\/li><li><code>4 &lt;= words[i].length &lt;= 50<\/code><\/li><li><code>1 &lt;= puzzles.length &lt;= 10^4<\/code><\/li><li><code>puzzles[i].length == 7<\/code><\/li><li><code>words[i][j]<\/code>,&nbsp;<code>puzzles[i][j]<\/code>&nbsp;are English lowercase letters.<\/li><li>Each&nbsp;<code>puzzles[i]&nbsp;<\/code>doesn&#8217;t contain repeated characters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Subsets<\/strong><\/h2>\n\n\n\n<p>Preprocessing: <br>Compress each word to a bit map, and compute the frequency of each bit map.<br>Since there are at most |words| bitmaps while its value ranging from 0 to 2^26, thus it&#8217;s better to use a hashtable instead of an array.<\/p>\n\n\n\n<p>Query:<br>Use the same way to compress a puzzle into a bit map.<br>Try all subsets (at most 128) of the puzzle (the bit of the first character is be must), and check how many words match each subset.<\/p>\n\n\n\n<p>words = [&#8220;aaaa&#8221;,&#8221;asas&#8221;,&#8221;able&#8221;,&#8221;ability&#8221;,&#8221;actt&#8221;,&#8221;actor&#8221;,&#8221;access&#8221;], <br>puzzle = &#8220;abslute&#8221;<br>bitmap(&#8220;aaaa&#8221;)  = {0}<br>bitmap(&#8220;asas&#8221;) = {0, 18}<br>bitmap(&#8220;able&#8221;) = {0,1,4,11}<br>bitmap(&#8220;actt&#8221;) = {0, 2, 19}<br>bitmap(&#8220;actor&#8221;) = {0, 2, 14, 17, 19}<br>bitmap(&#8220;access&#8221;) = {0, 2, 4, 18}<br><br>bitmap(&#8220;abslute&#8221;) = {0, 1, 4, 11, 18, 19, 20}<br><\/p>\n\n\n\n<p>Time complexity: O(sum(len(w_i)) + |puzzles|)<br>Space complexity: O(|words|)<\/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, 136 ms, 29.7 MB\nclass Solution {\npublic:\n  vector<int> findNumOfValidWords(vector<string>& words, vector<string>& puzzles) {\n    vector<int> ans;    \n    unordered_map<int, int> freq;\n    for (const string& word : words) {\n      int mask = 0;\n      for (char c : word)\n        mask |= 1 << (c - 'a');\n      ++freq[mask];\n    }    \n    for (const string&#038; p : puzzles) {\n      int mask = 0;      \n      for (char c : p)\n        mask |= 1 << (c - 'a');\n      int first = p[0] - 'a';\n      int curr = mask;\n      int total = 0;\n      while (curr) {\n        if ((curr >> first) & 1) {\n          auto it = freq.find(curr);\n          if (it != freq.end()) total += it->second;\n        }\n        curr = (curr - 1) & mask;\n      }\n      ans.push_back(total);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>With respect to a given&nbsp;puzzle&nbsp;string, a&nbsp;word&nbsp;is&nbsp;valid&nbsp;if both the following conditions are satisfied: word&nbsp;contains the first letter of&nbsp;puzzle. For each letter in&nbsp;word, that letter is in&nbsp;puzzle.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":[270,24,217,82,4,493],"class_list":["post-5521","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-compression","tag-frequency","tag-hard","tag-hashtable","tag-string","tag-subset","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5521","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=5521"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5521\/revisions"}],"predecessor-version":[{"id":5525,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5521\/revisions\/5525"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}