<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 {
public:

    int usingDP(string s) {
        int n = s.size();
        // int *dp = new int[n];
        int prev = 0;
        int bsSoFar = 0;
        if(s[0] == 'b') {
            // dp[0] = 0;
            prev = 0;
            bsSoFar++;
        }
        else {
            // dp[0] = 0;
            prev = 0;
        }
        for(int i=1; i<n; i++) {
            if(s[i] == 'b') {
                // dp[i] = dp[i-1];
                bsSoFar++;
                continue;
            }
            int keepA = bsSoFar;
            // int notKeepA = dp[i-1] + 1;
            int notKeepA = prev + 1;
            // dp[i] = min(keepA, notKeepA);
            prev = min(keepA, notKeepA);
        }
        // return dp[n-1];
        return prev;
    }

    int minimumDeletions(string s) {
        // we can delete all a's in between b's
        // or delete all b's that come in a
        // do this for every substring that contains both a and b
        return usingDP(s);
        int i=0, n = s.size();
        // int totala = 0;
        int aAfter = 0;
        while(i < n) {
            if(s[i] == 'a') aAfter++;
            i++;
        }
        i=0;
        int bBefore = 0;
        int minn = INT_MAX;
        while(i<n) {
            // int aAfter = totala - cura;
            if(s[i] == 'a')
                aAfter--; // this is done so that only a's that come after the current index are counted, not the current index if it is 'a'
            // int bBefore = i - cura;  // not i+1 so that b's before this index is counted, not the current index if it is 'b'
            minn = min(minn, bBefore + aAfter);
            // at the currentindex we can place anything, as after it there are all b's, and before all a's ==>   aaa..'a'bbbb.. , aaa..'b'bbbb.. --> both are satisfied
            if(s[i] == 'b')
                bBefore++;        
            i++;
        }
        return minn;
    }
};
27370