<?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>binary string Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/binary-string/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/binary-string/</link>
	<description></description>
	<lastBuildDate>Sun, 05 Apr 2020 07:25:16 +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>binary string Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/binary-string/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1404. Number of Steps to Reduce a Number in Binary Representation to One</title>
		<link>https://zxi.mytechroad.com/blog/bit/leetcode-1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/</link>
					<comments>https://zxi.mytechroad.com/blog/bit/leetcode-1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 05 Apr 2020 07:22:53 +0000</pubDate>
				<category><![CDATA[Bit]]></category>
		<category><![CDATA[big integer]]></category>
		<category><![CDATA[binary string]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[medieum]]></category>
		<category><![CDATA[O(n)]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6578</guid>

					<description><![CDATA[<p>Given a number&#160;s&#160;in their binary representation. Return the number of steps to reduce it to 1 under the following rules: If the current number is&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/">花花酱 LeetCode 1404. Number of Steps to Reduce a Number in Binary Representation to One</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>Given a number&nbsp;<code>s</code>&nbsp;in their binary representation. Return the number of steps to reduce it to 1 under the following rules:</p>



<ul><li>If the current number is even, you have to divide it by 2.</li><li>If the current number is odd, you have to add 1 to it.</li></ul>



<p>It&#8217;s guaranteed that you can always reach to one for all testcases.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "1101"
<strong>Output:</strong> 6
<strong>Explanation:</strong> "1101" corressponds to number 13 in their decimal representation.
Step 1) 13 is odd, add 1 and obtain 14.&nbsp;
Step 2) 14 is even, divide by 2 and obtain 7.
Step 3) 7 is odd, add 1 and obtain 8.
Step 4) 8 is even, divide by 2 and obtain 4.&nbsp; 
Step 5) 4 is even, divide by 2 and obtain 2.&nbsp;
Step 6) 2 is even, divide by 2 and obtain 1.&nbsp; 
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "10"
<strong>Output:</strong> 1
<strong>Explanation:</strong> "10" corressponds to number 2 in their decimal representation.
Step 1) 2 is even, divide by 2 and obtain 1.&nbsp; 
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "1"
<strong>Output:</strong> 0
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= s.length&nbsp;&lt;= 500</code></li><li><code>s</code>&nbsp;consists of characters &#8216;0&#8217; or &#8216;1&#8217;</li><li><code>s[0] == '1'</code></li></ul>



<h2><strong>Solution: Simulation</strong></h2>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int numSteps(string s) {
    int ans = 0;
    int carry = 0;
    // The highest bit must be 1, 
    // process to the 2nd highest bit
    for (int i = s.length() - 1; i &gt; 0; --i) {
      if (s[i] - '0' + carry == 1) {
        ans += 2; // odd: +1, even: / 2
        carry = 1; // always has a carry
      } else {
        ans += 1; // even: / 2
        // carray remains e.g. 1 + 1 = 10, or 0 + 0 = 00
      }
    }
    // If there is a carry, then it's even, one more step.
    return ans + carry;
  }
};</pre>
</div></div>



<div class="responsive-tabs">
<h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def numSteps(self, s: str) -&gt; int:
    ans, carry = 0, 0
    for i in range(1, len(s)):
      if ord(s[-i]) - ord('0') + carry == 1:
        ans += 2
        carry = 1
      else:
        ans += 1
    return ans + carry</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/">花花酱 LeetCode 1404. Number of Steps to Reduce a Number in Binary Representation to One</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/bit/leetcode-1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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>
