<p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>

<ul>
	<li>Remove substring <code>&quot;ab&quot;</code> and gain <code>x</code> points.

	<ul>
		<li>For example, when removing <code>&quot;ab&quot;</code> from <code>&quot;c<u>ab</u>xbae&quot;</code> it becomes <code>&quot;cxbae&quot;</code>.</li>
	</ul>
	</li>
	<li>Remove substring <code>&quot;ba&quot;</code> and gain <code>y</code> points.
	<ul>
		<li>For example, when removing <code>&quot;ba&quot;</code> from <code>&quot;cabx<u>ba</u>e&quot;</code> it becomes <code>&quot;cabxe&quot;</code>.</li>
	</ul>
	</li>
</ul>

<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>

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

<pre>
<strong>Input:</strong> s = &quot;cdbcbbaaabab&quot;, x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the &quot;ba&quot; underlined in &quot;cdbcbbaaa<u>ba</u>b&quot;. Now, s = &quot;cdbcbbaaab&quot; and 5 points are added to the score.
- Remove the &quot;ab&quot; underlined in &quot;cdbcbbaa<u>ab</u>&quot;. Now, s = &quot;cdbcbbaa&quot; and 4 points are added to the score.
- Remove the &quot;ba&quot; underlined in &quot;cdbcb<u>ba</u>a&quot;. Now, s = &quot;cdbcba&quot; and 5 points are added to the score.
- Remove the &quot;ba&quot; underlined in &quot;cdbc<u>ba</u>&quot;. Now, s = &quot;cdbc&quot; and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>

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

<pre>
<strong>Input:</strong> s = &quot;aabbaaxybbaabb&quot;, x = 5, y = 4
<strong>Output:</strong> 20
</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>1 &lt;= x, y &lt;= 10<sup>4</sup></code></li>
	<li><code>s</code> consists of lowercase English letters.</li>
</ul>

class Solution {
public:
    // int helper(string s, int x, int y, int i, int n, vector<char> &v){
    //     if(i>=n) return 0;
    //     int temp1=0, temp2=0, temp3=0, temp4=0, temp5=0;
        
    //     if(v.size()>0 && s[i]=='b' && v.back()=='a'){
    //         char c = v.back();
    //         v.pop_back();
    //         temp1 = x+helper(s, x, y, i+1, n, v);
    //         v.push_back(c);
    //         v.push_back(s[i]);
    //         temp2 = helper(s, x, y, i+1, n, v);
    //     }
    //     else if(v.size()>0 && s[i]=='a' && v.back()=='b'){
    //         char c = v.back();
    //         v.pop_back();
    //         temp3 = y+helper(s, x, y, i+1, n, v);
    //         v.push_back(c);
    //         v.push_back(s[i]);
    //         temp4 = helper(s, x, y, i+1, n, v);
    //     }
    //     else{
    //         v.push_back(s[i]);
    //         temp5 = helper(s, x, y, i+1, n, v);
    //     }
    //     return max(temp5, max(max(temp1, temp2), max(temp3, temp4)));
    // }
    int helper1(string s, char a, char b, int p, vector<char> &v){
        int n = s.size(), ans=0;
        v.push_back(s[0]);
        for(int i=1; i<n; i++){
            if(v.empty()==false && s[i]==b && v.back()==a){
                v.pop_back();
                ans += p;
            }
            else v.push_back(s[i]);
        }
        return ans;
    }
    int helper2(vector<char> &v, int a, int b, int m){
        vector<char> v2;
        v2.push_back(v[0]);
        int n = v.size(), ans=0;
        for(int i=1; i<n; i++){
            if(v2.empty()==false && v[i]==b && v2.back()==a){
                v2.pop_back();
                ans += m;
            }
            else v2.push_back(v[i]);
        }
        return ans;
    }
    int maximumGain(string s, int x, int y)
    {
        char a='a', b='b';
        if(y>x){
            a = 'b';
            b = 'a';
        }
        vector<char> v;
        int p = max(x,y), m=min(x,y);
        int ans1 = helper1(s, a, b, p, v);

        int n = v.size();
        for(int i=0; i<n; i++) cout<<v[i]<<" ";
        
        int ans2 = helper2(v, b, a, m);
        return ans1+ans2;
    }
    // {
    //     vector<char> v;
    //     v.push_back(s[0]);
    //     return helper(s, x, y, 1, s.size(), v);

    // }
};
28890