{"id":1994,"date":"2018-03-06T09:15:56","date_gmt":"2018-03-06T17:15:56","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1994"},"modified":"2018-03-06T09:34:25","modified_gmt":"2018-03-06T17:34:25","slug":"leetcode-696-count-binary-substrings","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-696-count-binary-substrings\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 696. Count Binary Substrings"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u8fdb\u5236\u7684\u5b57\u7b26\u4e32\uff0c\u95ee\u6709\u591a\u5c11\u5b50\u4e32\u76840\u4e2a\u6570\u91cf\u7b49\u4e8e1\u7684\u6570\u91cf\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/count-binary-substrings\/description\/\">https:\/\/leetcode.com\/problems\/count-binary-substrings\/description\/<\/a><\/p>\n<p>Give a string\u00a0<code>s<\/code>, count the number of non-empty (contiguous) substrings that have the same number of 0&#8217;s and 1&#8217;s, and all the 0&#8217;s and all the 1&#8217;s in these substrings are grouped consecutively.<\/p>\n<p>Substrings that occur multiple times are counted the number of times they occur.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: \"00110011\"\r\nOutput: 6\r\nExplanation: There are 6 substrings that have equal number of consecutive 1's and 0's: \"0011\", \"01\", \"1100\", \"10\", \"0011\", and \"01\".\r\n\r\nNotice that some of these substrings repeat and are counted the number of times they occur.\r\n\r\nAlso, \"00110011\" is not a valid substring because all the 0's (and 1's) are not grouped together.\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: \"10101\"\r\nOutput: 4\r\nExplanation: There are 4 substrings: \"10\", \"01\", \"10\", \"01\" that have equal number of consecutive 1's and 0's.\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li><code>s.length<\/code>\u00a0will be between 1 and 50,000.<\/li>\n<li><code>s<\/code>\u00a0will only consist of &#8220;0&#8221; or &#8220;1&#8221; characters.<\/li>\n<\/ul>\n<p><strong>Solution 0: Search<\/strong><\/p>\n<p>Try all possible substrings and check whether it&#8217;s a valid one or not.<\/p>\n<p>Time complexity O(2^n * n) TLE<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Solution 1: Running Length<\/strong><\/p>\n<p>For S = &#8220;000110&#8221;, there are two virtual blocks &#8220;[00011]0&#8221; and\u00a0&#8220;000[110]&#8221; which contains consecutive 0s and 1s or (1s and 0s)<\/p>\n<p>Keep tracking of the running length of 0, 1 for each block<\/p>\n<p>&#8220;00011&#8221; =&gt; &#8216;0&#8217;: 3, &#8216;1&#8217;:2<\/p>\n<p>ans += min(3, 2) =&gt; ans = 2 (&#8220;[0011]&#8221;, &#8220;0[01]1&#8221;)<\/p>\n<p>&#8220;110&#8221; =&gt; &#8216;0&#8217;: 1, &#8216;1&#8217;: 2<\/p>\n<p>ans += min(1, 2) =&gt; ans = 3\u00a0(&#8220;[0011]&#8221;, &#8220;0[01]1&#8221;, &#8220;1[10]&#8221;)<\/p>\n<p>We can reuse part of the running length from last block.<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 43 ms\r\nclass Solution {\r\npublic:\r\n  int countBinarySubstrings(string s) {        \r\n    vector&lt;int&gt; lens(128, 0);\r\n    int i = 0;\r\n    int l = 0;\r\n    int ans = 0;\r\n    while (true) {\r\n      while (i &lt; s.length() &amp;&amp; s[i] == s[l]) ++i;      \r\n      lens[s[l]] = i - l;\r\n      ans += min(lens['0'], lens['1']);\r\n      if (i == s.length()) break;\r\n      lens[s[i]] = 0;\r\n      l = i;\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u4e8c\u8fdb\u5236\u7684\u5b57\u7b26\u4e32\uff0c\u95ee\u6709\u591a\u5c11\u5b50\u4e32\u76840\u4e2a\u6570\u91cf\u7b49\u4e8e1\u7684\u6570\u91cf\u3002 Problem: https:\/\/leetcode.com\/problems\/count-binary-substrings\/description\/ Give a string\u00a0s, count the number of non-empty (contiguous) substrings that have the same number of 0&#8217;s and 1&#8217;s, and all the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[235,222,234,4],"class_list":["post-1994","post","type-post","status-publish","format-standard","hentry","category-string","tag-binary-string","tag-easy","tag-running-length","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1994","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=1994"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1994\/revisions"}],"predecessor-version":[{"id":1997,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1994\/revisions\/1997"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}