<p>You are given a string <code>s</code> consisting only of characters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>​​​​.</p>

<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i &lt; j</code> and <code>s[i] = &#39;b&#39;</code> and <code>s[j]= &#39;a&#39;</code>.</p>

<p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p>

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

<pre>
<strong>Input:</strong> s = &quot;aababbab&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can either:
Delete the characters at 0-indexed positions 2 and 6 (&quot;aa<u>b</u>abb<u>a</u>b&quot; -&gt; &quot;aaabbb&quot;), or
Delete the characters at 0-indexed positions 3 and 6 (&quot;aab<u>a</u>bb<u>a</u>b&quot; -&gt; &quot;aabbbb&quot;).
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;bbaaaaabb&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> The only solution is to delete the first two characters.
</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&nbsp;<code>&#39;a&#39;</code> or <code>&#39;b&#39;</code>​​.</li>
</ul>

class Solution:
    def minimumDeletions(self, s: str) -> int:
        b_before = [0]*len(s)
        a_after = [0]*len(s)
        count=0
        for i,c in enumerate(s):
            b_before[i] = count
            if c == 'b':
                count+=1
        count=0
        for i in range(len(s)-1,-1,-1):
            a_after[i] = count
            if s[i] == 'a':
                count+=1
        to_remove = 0
        # print(list(s))
        # print(b_before)
        # print(a_after)
        a_removed = 0
        b_removed = 0
        res = []
        last_seen_a,last_seen_b =0,0
        for i in range(1,len(s)):
            temp = 0
            
            if s[i-1]=='a' and s[i]=='b':
                # what if remove all the a behind i and all the b before i-1
                temp=temp + a_after[i]
                temp=temp + b_before[i-1]
                res.append(temp)

            if s[i-1]=='b' and s[i]=='a':
                # if keep b, remove all the a behind i-1 and all b before this b session
                temp1= a_after[i-1]
                temp1 = temp1+b_before[last_seen_a]
                # if keep a, remove  all b before this a  and all a after this a session
                temp2 = b_before[i]
                j=i
                while j<len(s) and s[j]=='a':
                    j+=1
                if j<(len(s)):    
                    temp2 = temp2 + a_after[j]

                res.append(min(temp1,temp2))
            if s[i]=='a':
                last_seen_a = i
            else:
                last_seen_b = i
        #print(res)
        if res:
            return  min(res)
        return 0
            
                    
                
        return res

            
        
        
        
23878