<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 print0(string &b,int c,int e,char temp0){
        if(c==e){
            return 0;
        }
        int temp2=0;
        if(temp0=='a'){
            if(b[c]=='a'){
                temp2=temp2+print0(b,c+1,e,'a');
            }
            else if(b[c]=='b'){
                temp2=min(print0(b,c+1,e,'a')+1,print0(b,c+1,e,'b'));
            }
        }
        else if(temp0=='b'){
            if(b[c]=='a'){
                temp2=print0(b,c+1,e,'b')+1;
            }
            else if(b[c]=='b'){
                temp2=print0(b,c+1,e,'b');
            }
        }
        return temp2;
    }*/
    int print0(string &b,int c,int e,/*vector<vector<int>> &temp0*/int temp0[100002][2],char temp2){
        if(c==e){
            return 0;
        }
        int temp12=0;
        if(temp2=='a'){
            if(temp0[c][0]!=-1){
                return temp0[c][0];
            }
        }
        else if(temp2=='b'){
            if(temp0[c][1]!=-1){
                return temp0[c][1];
            }
        }
        if(temp2=='a'){
            if(b[c]=='a'){
                temp12=print0(b,c+1,e,temp0,'a');
            }
            else if(b[c]=='b'){
                temp12=min(print0(b,c+1,e,temp0,'a')+1,print0(b,c+1,e,temp0,'b'));
            }
        }
        else if(temp2=='b'){
            if(b[c]=='a'){
                temp12=print0(b,c+1,e,temp0,'b')+1;
            }
            else if(b[c]=='b'){
                temp12=print0(b,c+1,e,temp0,'b');
            }
        }
        if(temp2=='a'){
            temp0[c][0]=temp12;
        }
        else if(temp2=='b'){
            temp0[c][1]=temp12;
        }
        return temp12;
    }
    int minimumDeletions(string s) {
         /*int b=0;
         int c=s.size();
         string temp0=s;
         char temp2='a';
         int e=0;
         e=print0(temp0,b,c,temp2);
         return e;*/
         int b=0;
         int c=s.size();
         string temp0=s;
         char temp2='a';
         //vector<vector<int>> temp12;
         int temp12[100002][2];
         for(int temp18=0;temp18<s.size();temp18++){
            //vector<int> temp28;
            for(int temp48=0;temp48<2;temp48++){
                //temp28.push_back(-1);
                temp12[temp18][temp48]=-1;
            }
            //temp12.push_back(temp28);
         }
         int temp18=0;
         temp18=print0(temp0,b,c,temp12,temp2);
         return temp18;
    }
};
35770