<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>running length Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/running-length/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/running-length/</link>
	<description></description>
	<lastBuildDate>Tue, 06 Mar 2018 17:34:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>running length Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/running-length/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 696. Count Binary Substrings</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-696-count-binary-substrings/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-696-count-binary-substrings/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 06 Mar 2018 17:15:56 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[binary string]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[running length]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1994</guid>

					<description><![CDATA[<p>题目大意：给你一个二进制的字符串，问有多少子串的0个数量等于1的数量。 Problem: https://leetcode.com/problems/count-binary-substrings/description/ Give a string s, 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>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-696-count-binary-substrings/">花花酱 LeetCode 696. Count Binary Substrings</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>题目大意：给你一个二进制的字符串，问有多少子串的0个数量等于1的数量。</p>
<p><strong>Problem:</strong></p>
<p><a href="https://leetcode.com/problems/count-binary-substrings/description/">https://leetcode.com/problems/count-binary-substrings/description/</a></p>
<p>Give a string <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>
<p>Substrings that occur multiple times are counted the number of times they occur.</p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input: &quot;00110011&quot;
Output: 6
Explanation: There are 6 substrings that have equal number of consecutive 1's and 0's: &quot;0011&quot;, &quot;01&quot;, &quot;1100&quot;, &quot;10&quot;, &quot;0011&quot;, and &quot;01&quot;.

Notice that some of these substrings repeat and are counted the number of times they occur.

Also, &quot;00110011&quot; is not a valid substring because all the 0's (and 1's) are not grouped together.</pre><p><b>Example 2:</b></p><pre class="crayon-plain-tag">Input: &quot;10101&quot;
Output: 4
Explanation: There are 4 substrings: &quot;10&quot;, &quot;01&quot;, &quot;10&quot;, &quot;01&quot; that have equal number of consecutive 1's and 0's.</pre><p><b>Note:</b></p>
<p>&nbsp;</p>
<ul>
<li><code>s.length</code> will be between 1 and 50,000.</li>
<li><code>s</code> will only consist of &#8220;0&#8221; or &#8220;1&#8221; characters.</li>
</ul>
<p><strong>Solution 0: Search</strong></p>
<p>Try all possible substrings and check whether it&#8217;s a valid one or not.</p>
<p>Time complexity O(2^n * n) TLE</p>
<p>Space complexity: O(n)</p>
<p>&nbsp;</p>
<p><strong>Solution 1: Running Length</strong></p>
<p>For S = &#8220;000110&#8221;, there are two virtual blocks &#8220;[00011]0&#8221; and &#8220;000[110]&#8221; which contains consecutive 0s and 1s or (1s and 0s)</p>
<p>Keep tracking of the running length of 0, 1 for each block</p>
<p>&#8220;00011&#8221; =&gt; &#8216;0&#8217;: 3, &#8216;1&#8217;:2</p>
<p>ans += min(3, 2) =&gt; ans = 2 (&#8220;[0011]&#8221;, &#8220;0[01]1&#8221;)</p>
<p>&#8220;110&#8221; =&gt; &#8216;0&#8217;: 1, &#8216;1&#8217;: 2</p>
<p>ans += min(1, 2) =&gt; ans = 3 (&#8220;[0011]&#8221;, &#8220;0[01]1&#8221;, &#8220;1[10]&#8221;)</p>
<p>We can reuse part of the running length from last block.</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 43 ms
class Solution {
public:
  int countBinarySubstrings(string s) {        
    vector&lt;int&gt; lens(128, 0);
    int i = 0;
    int l = 0;
    int ans = 0;
    while (true) {
      while (i &lt; s.length() &amp;&amp; s[i] == s[l]) ++i;      
      lens[s[l]] = i - l;
      ans += min(lens['0'], lens['1']);
      if (i == s.length()) break;
      lens[s[i]] = 0;
      l = i;
    }
    return ans;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-696-count-binary-substrings/">花花酱 LeetCode 696. Count Binary Substrings</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/string/leetcode-696-count-binary-substrings/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
