<p>Given a binary string <code>s</code>, return the number of non-empty substrings that have the same number of <code>0</code>&#39;s and <code>1</code>&#39;s, and all the <code>0</code>&#39;s and all the <code>1</code>&#39;s in these substrings are grouped consecutively.</p>

<p>Substrings that occur multiple times are counted the number of times they occur.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;00110011&quot;
<strong>Output:</strong> 6
<strong>Explanation:</strong> There are 6 substrings that have equal number of consecutive 1&#39;s and 0&#39;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&#39;s (and 1&#39;s) are not grouped together.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;10101&quot;
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are 4 substrings: &quot;10&quot;, &quot;01&quot;, &quot;10&quot;, &quot;01&quot; that have equal number of consecutive 1&#39;s and 0&#39;s.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
	<li><code>s[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
</ul>

class Solution:
    def countBinarySubstrings(self, s: str) -> int:
        q = deque()
        res = 0
        for i in range(len(s)):
            while q and s[i] == q[-1] and s[i]!=q[0]:
                q.pop()
            if q and s[i]!=q[-1]:
                res+=1
                q.pop()
            q.appendleft(s[i])
        return res












        q = deque()
        res = 0
        for i in range(len(s)):
            while q and s[i] == q[-1] and q[0] != s[i]:
                q.pop()
            if q and s[i]!=q[-1]:
                res+=1
                q.pop()
            q.appendleft(s[i]) 
        return res
















        q = deque()
        res = 0
        for i in range(len(s)):
            while q and s[i]!=q[0] and s[i] == q[-1]:
                q.pop()
            if q and  s[i]!=q[-1]:
                q.pop()
                res+=1
            q.appendleft(s[i])
        return res
















        q = deque()
        res = 0
        for i in range(len(s)):
            while q  and s[i]!=q[0] and q[-1] == s[i]:
                q.pop()
            if q and s[i]!=q[-1]:
                q.pop()
                res+=1
            q.appendleft(s[i])
        return res















        q = deque()
        res = 0
        for i in range(len(s)):
            while q and q[0]!=s[i] and s[i]==q[-1]:
                q.pop()
            
            if q and q[-1]!=s[i]:
                q.pop()
                res+=1
            q.appendleft(s[i])
        return res
















        q = deque()
        count = 0
        for  i in range(len(s)):
            while q and q[-1]==s[i] and q[0] !=s[i]:
                q.pop()
            if q and q[-1]!=s[i]:
                q.pop()
                count+=1
            q.appendleft(s[i])
        return count
















        q = deque()
        count = 0
        for i in range(len(s)):
            while q and q[0]!=s[i] and s[i] == q[-1]:
                q.pop()
            if q and s[i] !=q[-1]:
                count+=1
                q.pop()
            q.appendleft(s[i])
        return count























        q = deque()
        count = 0
        for i in range(len(s)):
            while q and q[0]!=s[i] and q[-1] == s[i]:
                q.pop()
            if q and q[-1]!=s[i]:
                q.pop()
                count+=1
            q.appendleft(s[i])
        return count
            





















        q = deque()
        count = 0
        for i in range(len(s)):
            while q and q[0]!=s[i] and q[-1] == s[i]:
                q.pop()
            if q and q[-1]!=s[i]:
                count+=1
                q.pop()
            q.appendleft(s[i])
        return count






















        q = deque()
        count = 0
        for i in range(len(s)):
            while q and q[0]!=s[i] and q[-1]==s[i]:
                q.pop()
            if q and q[-1]!=s[i]:
                q.pop()
                count+=1

            q.appendleft(s[i])
        return count
        res,prev,cur = 0,0,1
        for i in range(1,len(s)):
            if s[i]!=s[i-1]:
                res+=min(prev,cur)
                prev = cur
                cur = 1
            else:
                cur+=1
        return res + min(prev,cur)

















        count =1
        precount = 0
        res = 0

        for i in range(1,len(s)):
            if s[i] == s[i-1]:
                count+=1
            else:
                print(count,precount,"1")
                precount = count
                print(count,precount,"2")
                count=1
                print(count,precount,"3")
            if precount>=count:
                res+=1
        return res

            



        
17400