<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 {
    int findMiniDel(vector<int> &a_count, vector<int> &b_sum, string &reduced_s, int a_pos, int b_pos) {
        if(a_pos<0 || b_pos<0) {
            return 0;
        }
        // findMiniDel(a_count, b_sum, string &reduced_s, pos-1)
        int delete_a = a_count[a_pos] + findMiniDel(a_count, b_sum, reduced_s, a_pos-1, b_pos-1);
        int donot_delete_a = b_sum[b_pos];
        return min(delete_a, donot_delete_a);
    }
public:
    int minimumDeletions(string s) {
        vector<int> a_count, b_sum;
        string reduced_s;
        char current_char=s[0];
        int cur_counter=1, last_num;
        if(s[0] == 'b') {
            reduced_s.push_back(s[0]);
        }
        
        for(int i=1; i<s.length(); i++) {
            if(current_char == s[i]) {
                cur_counter++;
            } else {
                
                
                if(current_char == 'a') {
                    // if(a_sum.size()==0) {
                    //     last_num = 0;
                    // } else {
                    //     last_num = a_count[a_count.size()-1];
                    // }
                    if(reduced_s != "") {
                        // cout<<"\n pushing"
                        a_count.push_back(cur_counter);
                    }
                    
                    
                } else {
                    // cout<<"\nfound b\n";
                    if(b_sum.size()==0) {
                        last_num = 0;
                    } else {
                        last_num = b_sum[b_sum.size()-1];
                    }
                    b_sum.push_back(last_num+cur_counter);

                }
                cur_counter = 1;
                current_char = s[i];
                reduced_s.push_back(s[i]);
            }
        }
        if(current_char == 'a') {
            a_count.push_back(cur_counter);
        }


        // if(reduced_s[reduced_s.length()-1] == 'b') {
        //     reduced_s.pop_back();
        //     b_sum.pop_back();
        // }


        // cout<<"\nreduced s: "<<reduced_s;
        // cout<<"\na_counts: ";
        // for(auto x:a_count) {
        //     cout<<x<<" ";
        // }
        // cout<<"\nb_sums: ";
        // for(auto x:b_sum) {
        //     cout<<x<<" ";
        // }
        return findMiniDel(a_count, b_sum, reduced_s, a_count.size()-1, b_sum.size()-1);
        
    }
};
50890