{"id":9298,"date":"2021-12-31T05:03:44","date_gmt":"2021-12-31T13:03:44","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9298"},"modified":"2021-12-31T05:04:00","modified_gmt":"2021-12-31T13:04:00","slug":"leetcode-1935-maximum-number-of-words-you-can-type","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1935-maximum-number-of-words-you-can-type\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1935. Maximum Number of Words You Can Type"},"content":{"rendered":"\n<p>There is a malfunctioning keyboard where some letter keys do not work. All other keys on the keyboard work properly.<\/p>\n\n\n\n<p>Given a string&nbsp;<code>text<\/code>&nbsp;of words separated by a single space (no leading or trailing spaces) and a string&nbsp;<code>brokenLetters<\/code>&nbsp;of all&nbsp;<strong>distinct<\/strong>&nbsp;letter keys that are broken, return&nbsp;<em>the&nbsp;<strong>number of words<\/strong>&nbsp;in<\/em>&nbsp;<code>text<\/code>&nbsp;<em>you can fully type using this keyboard<\/em>.<\/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> text = \"hello world\", brokenLetters = \"ad\"\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> We cannot type \"world\" because the 'd' key is broken.\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> text = \"leet code\", brokenLetters = \"lt\"\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> We cannot type \"leet\" because the 'l' and 't' keys are broken.\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> text = \"leet code\", brokenLetters = \"e\"\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> We cannot type either word because the 'e' key is broken.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= text.length &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>0 &lt;= brokenLetters.length &lt;= 26<\/code><\/li><li><code>text<\/code>&nbsp;consists of words separated by a single space without any leading or trailing spaces.<\/li><li>Each word only consists of lowercase English letters.<\/li><li><code>brokenLetters<\/code>&nbsp;consists of&nbsp;<strong>distinct<\/strong>&nbsp;lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashset \/ bitset<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<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 canBeTypedWords(string text, string brokenLetters) {\n    const int n = text.size();\n    \n    bitset<26> b;\n    for (char c : brokenLetters) b.set(c - 'a');\n    \n    int ans = 0;\n    for (int i = 0, broken = 0; i <= n; ++i) {\n      if (i == n || text[i] == ' ') {\n        if (!broken) ++ans;\n        broken = 0;\n      } else {\n        broken |= b[text[i] - 'a'];\n      }\n    }\n    \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is a malfunctioning keyboard where some letter keys do not work. All other keys on the keyboard work properly. Given a string&nbsp;text&nbsp;of words separated&#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":[232,222,82,4],"class_list":["post-9298","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-bitset","tag-easy","tag-hashtable","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9298","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=9298"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9298\/revisions"}],"predecessor-version":[{"id":9300,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9298\/revisions\/9300"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}