{"id":9428,"date":"2022-01-17T19:12:42","date_gmt":"2022-01-18T03:12:42","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9428"},"modified":"2022-01-17T19:13:04","modified_gmt":"2022-01-18T03:13:04","slug":"leetcode-2135-count-words-obtained-after-adding-a-letter","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2135-count-words-obtained-after-adding-a-letter\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2135. Count Words Obtained After Adding a Letter"},"content":{"rendered":"\n<p>You are given two&nbsp;<strong>0-indexed<\/strong>&nbsp;arrays of strings&nbsp;<code>startWords<\/code>&nbsp;and&nbsp;<code>targetWords<\/code>. Each string consists of&nbsp;<strong>lowercase English letters<\/strong>&nbsp;only.<\/p>\n\n\n\n<p>For each string in&nbsp;<code>targetWords<\/code>, check if it is possible to choose a string from&nbsp;<code>startWords<\/code>&nbsp;and perform a&nbsp;<strong>conversion operation<\/strong>&nbsp;on it to be equal to that from&nbsp;<code>targetWords<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<strong>conversion operation<\/strong>&nbsp;is described in the following two steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Append<\/strong>&nbsp;any lowercase letter that is&nbsp;<strong>not present<\/strong>&nbsp;in the string to its end.<ul><li>For example, if the string is&nbsp;<code>\"abc\"<\/code>, the letters&nbsp;<code>'d'<\/code>,&nbsp;<code>'e'<\/code>, or&nbsp;<code>'y'<\/code>&nbsp;can be added to it, but not&nbsp;<code>'a'<\/code>. If&nbsp;<code>'d'<\/code>&nbsp;is added, the resulting string will be&nbsp;<code>\"abcd\"<\/code>.<\/li><\/ul><\/li><li><strong>Rearrange<\/strong>&nbsp;the letters of the new string in&nbsp;<strong>any<\/strong>&nbsp;arbitrary order.<ul><li>For example,&nbsp;<code>\"abcd\"<\/code>&nbsp;can be rearranged to&nbsp;<code>\"acbd\"<\/code>,&nbsp;<code>\"bacd\"<\/code>,&nbsp;<code>\"cbda\"<\/code>, and so on. Note that it can also be rearranged to&nbsp;<code>\"abcd\"<\/code>&nbsp;itself.<\/li><\/ul><\/li><\/ol>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>number of strings<\/strong>&nbsp;in&nbsp;<\/em><code>targetWords<\/code><em>&nbsp;that can be obtained by performing the operations on&nbsp;<strong>any<\/strong>&nbsp;string of&nbsp;<\/em><code>startWords<\/code>.<\/p>\n\n\n\n<p><strong>Note<\/strong>&nbsp;that you will only be verifying if the string in&nbsp;<code>targetWords<\/code>&nbsp;can be obtained from a string in&nbsp;<code>startWords<\/code>&nbsp;by performing the operations. The strings in&nbsp;<code>startWords<\/code>&nbsp;<strong>do not<\/strong>&nbsp;actually change during this process.<\/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> startWords = [\"ant\",\"act\",\"tack\"], targetWords = [\"tack\",\"act\",\"acti\"]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong>\n- In order to form targetWords[0] = \"tack\", we use startWords[1] = \"act\", append 'k' to it, and rearrange \"actk\" to \"tack\".\n- There is no string in startWords that can be used to obtain targetWords[1] = \"act\".\n  Note that \"act\" does exist in startWords, but we <strong>must<\/strong> append one letter to the string before rearranging it.\n- In order to form targetWords[2] = \"acti\", we use startWords[1] = \"act\", append 'i' to it, and rearrange \"acti\" to \"acti\" itself.\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> startWords = [\"ab\",\"a\"], targetWords = [\"abc\",\"abcd\"]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong>\n- In order to form targetWords[0] = \"abc\", we use startWords[0] = \"ab\", add 'c' to it, and rearrange it to \"abc\".\n- There is no string in startWords that can be used to obtain targetWords[1] = \"abcd\".\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= startWords.length, targetWords.length &lt;= 5 * 10<sup>4<\/sup><\/code><\/li><li><code>1 &lt;= startWords[i].length, targetWords[j].length &lt;= 26<\/code><\/li><li>Each string of&nbsp;<code>startWords<\/code>&nbsp;and&nbsp;<code>targetWords<\/code>&nbsp;consists of lowercase English letters only.<\/li><li>No letter occurs more than once in any string of&nbsp;<code>startWords<\/code>&nbsp;or&nbsp;<code>targetWords<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Bitmask w\/ Hashtable<\/strong><\/h2>\n\n\n\n<p>Since there is no duplicate letters in each word, we can use a bitmask to represent a word.<\/p>\n\n\n\n<p>Step 1: For each word in startWords, we obtain its bitmask and insert it into a hashtable.<br>Step 2: For each word in targetWords, enumerate it&#8217;s letter and unset 1 bit (skip one letter) and see whether it&#8217;s in the hashtable or not.<\/p>\n\n\n\n<p>E.g. for target word &#8220;abc&#8221;, its bitmask is 0&#8230;0111, and we test whether &#8220;ab&#8221; or &#8220;ac&#8221; or &#8220;bc&#8221; in the hashtable or not.<\/p>\n\n\n\n<p>Time complexity: O(n * 26^2)<br>Space complexity: O(n * 26)<\/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 wordCount(vector<string>& startWords, vector<string>& targetWords) {\n    unordered_set<int> s;\n    auto getKey = [](const string& s) {      \n      return accumulate(begin(s), end(s), 0, [](int key, char c) { return key | (1 << (c - 'a')); });\n    };\n    \n    for (const string&#038; w : startWords)\n      s.insert(getKey(w));\n        \n    return accumulate(begin(targetWords), end(targetWords), 0, [&#038;](int ans, const string&#038; w) {\n      const int key = getKey(w);\n      return ans + any_of(begin(w), end(w), [&#038;](char c) { return s.count(key ^ (1 << (c - 'a'))); });\n    });\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two&nbsp;0-indexed&nbsp;arrays of strings&nbsp;startWords&nbsp;and&nbsp;targetWords. Each string consists of&nbsp;lowercase English letters&nbsp;only. For each string in&nbsp;targetWords, check if it is possible to choose a string&#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":[621,82,177,4],"class_list":["post-9428","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-bitmask","tag-hashtable","tag-medium","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9428","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=9428"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9428\/revisions"}],"predecessor-version":[{"id":9430,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9428\/revisions\/9430"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}