<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 minimumDeletions(string s) {
        // 3 passes method time complexity O(n)
        // int n = s.length();
        // vector<int>countA(n, 0);
        // vector<int>countB(n, 0);
        // int b = 0;
        // // Count the number of B on the left of the current element
        // for(int i=0; i<n; i+=1){
        //     countB[i]=b;
        //     if(s[i]=='b'){
        //         b+=1;
        //     }
        // }
        // int a = 0;
        // // Count the number of A on the right of the current element
        // for(int i=n-1; i>=0; i-=1){
        //     countA[i]=a;
        //     if(s[i]=='a'){
        //         a+=1;
        //     }
        // }
        // int minDeletions = n;
        // // Number of deletions = num of Bs on the left of the current element + num of As on the right of the current element
        // for(int i=0; i<n; i+=1){
        //     minDeletions = min(minDeletions, countA[i]+countB[i]);
        // }
        // return minDeletions;

        int n = s.length();
        // Store the minimum deletions needed to balance the substrings
        vector<int>dp(n+1, 0);
        int b = 0;
        for(int i=0; i<n; i+=1){
            // Keep the number of characters to be deleted if it's b
            if(s[i]=='b'){
                dp[i+1]=dp[i];
                b+=1;
            }
            // Remove a case dp[i]+1, otherwise remove all b before the current a
            else{
                dp[i+1]=min(dp[i]+1, b);
            }
        }
        return dp[n];
    }
};
37450