{"id":6836,"date":"2020-05-30T16:26:06","date_gmt":"2020-05-30T23:26:06","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6836"},"modified":"2020-05-30T16:39:58","modified_gmt":"2020-05-30T23:39:58","slug":"leetcode-1461-check-if-a-string-contains-all-binary-codes-of-size-k","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1461-check-if-a-string-contains-all-binary-codes-of-size-k\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1461. Check If a String Contains All Binary Codes of Size K"},"content":{"rendered":"\n<p>Given a binary string&nbsp;<code>s<\/code>&nbsp;and an integer&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>True<\/em>&nbsp;if any binary code of length&nbsp;<code>k<\/code>&nbsp;is a substring of&nbsp;<code>s<\/code>. Otherwise, return&nbsp;<em>False<\/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> s = \"00110110\", k = 2\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> The binary codes of length 2 are \"00\", \"01\", \"10\" and \"11\". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively.\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> s = \"00110\", k = 2\n<strong>Output:<\/strong> true\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> s = \"0110\", k = 1\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> The binary codes of length 1 are \"0\" and \"1\", it is clear that both exist as a substring. \n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"0110\", k = 2\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> The binary code \"00\" is of length 2 and doesn't exist in the array.\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"0000000001011100\", k = 4\n<strong>Output:<\/strong> false\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length &lt;= 5 * 10^5<\/code><\/li><li><code>s<\/code>&nbsp;consists of 0&#8217;s and 1&#8217;s only.<\/li><li><code>1 &lt;= k &lt;= 20<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Insert all possible substrings into a hashtable, the size of the hashtable should be 2^k.<\/p>\n\n\n\n<p>Time complexity: O(n*k)<br>Space complexity: O(2^k*k) -&gt; O(2^k)<\/p>\n\n\n\n<p>std::string_view: 484 ms, 40.1MB<br>std::string 644 ms, 58.6MB<\/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\n\/\/ std::string_view: 468 ms, 37.3MB\n\/\/ std::string:      636 ms, 58.6MB\nclass Solution {\npublic:\n  bool hasAllCodes(string_view s, int k) {\n    const int n = s.length();\n    if ((n - k + 1) * k < (1 << k)) return false;\n    unordered_set<string_view> c;\n    for (int i = 0; i + k <= n; ++i)\n      c.insert(s.substr(i, k));    \n    return c.size() == (1 << k);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary string&nbsp;s&nbsp;and an integer&nbsp;k. Return&nbsp;True&nbsp;if any binary code of length&nbsp;k&nbsp;is a substring of&nbsp;s. Otherwise, return&nbsp;False. Example 1: Input: s = &#8220;00110110&#8221;, k =&#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":[82,4,314],"class_list":["post-6836","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-string","tag-substring","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6836","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=6836"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6836\/revisions"}],"predecessor-version":[{"id":6840,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6836\/revisions\/6840"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}