<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 maximumGain(string s, int x, int y) {
        string d;
        if(x>y) {
            d="ab";
        }
        else {
            d="ba";
        }
        vector<char>st;

        for(int i=0;i<s.length();i++) {
            if(st.size()==0) {
                st.push_back(s[i]);
                continue;
            }
            char f = st[st.size()-1];

            //string c = f+s[i];
            if(f==d[0] && s[i]==d[1]) {
                st.pop_back();

            }
            else {
                st.push_back(s[i]);
            }
        }
        string v ="";
        for(int i=0;i<st.size();i++) {
            v+=st[i];
        }
        int ans=0;
        if(d=="ab") {
            ans+=((s.length()-st.size())/2)*x;
            d="ba";
        }
        else {
            ans+=((s.length()-st.size())/2)*y;
            d="ab";
        }
        //cout<<ans<<endl;
        vector<char>h;
        for(int i=0;i<v.length();i++) {
            if(h.size()==0) {
                h.push_back(v[i]);
                continue;
            }
            char t = h[h.size()-1];
            //string g = t+v[i];
            if(t==d[0] && v[i]==d[1]) {
                h.pop_back();
            }
            else {
                h.push_back(v[i]);
            }

        }
        if(d=="ab") {
            ans+=((st.size()-h.size())/2)*x;
            
        }
        else {
            ans+=((st.size()-h.size())/2)*y;
           
        }
        //cout<<ans<<endl;
        return ans;

    }
};
// class Solution {
// public:
//     int maximumGain(string s, int x, int y) {
//         int n     = s.length();
//         int score = 0;

//         string maxStr = (x > y) ? "ab" : "ba";
//         string minStr = (x < y) ? "ab" : "ba";

//         //First Pass
//         string temp_first     = removeSubstring(s, maxStr);
//         int removedPairsCount = (n - temp_first.length()) / 2;
//         score                += removedPairsCount * max(x, y);
//         cout<<score<<endl;


//         //Second Pass
//         string temp_second = removeSubstring(temp_first, minStr);
//         removedPairsCount  = (temp_first.length() - temp_second.length()) / 2;
//         score             += removedPairsCount * min(x, y);
//         cout<<score<<endl;

//         return score;
//     }

// private:
//     string removeSubstring(string& s, string& matchStr) {
//         stack<char> st;

//         for (char &ch : s) {
//             if (ch == matchStr[1] && !st.empty() && st.top() == matchStr[0]) {
//                 st.pop();
//             } else {
//                 st.push(ch);
//             }
//         }

//         string remainStr;
//         while (!st.empty()) {
//             remainStr.push_back(st.top());
//             st.pop();
//         }
//         reverse(remainStr.begin(), remainStr.end());
//         return remainStr;
//     }
// };
31403