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


	Remove substring "ab" and gain x points.

	
		For example, when removing "ab" from "cabxbae" it becomes "cxbae".
	
	
	Remove substring "ba" and gain y points.
	
		For example, when removing "ba" from "cabxbae" it becomes "cabxe".
	
	


Return the maximum points you can gain after applying the above operations on s.

&nbsp;
Example 1:

Input: s = "cdbcbbaaabab", x = 4, y = 5
Output: 19
Explanation:
- Remove the "ba" underlined in "cdbcbbaaabab". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaaab". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcbbaa". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbcba". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.

Example 2:

Input: s = "aabbaaxybbaabb", x = 5, y = 4
Output: 20


&nbsp;
Constraints:


	1 &lt;= s.length &lt;= 105
	1 &lt;= x, y &lt;= 104
	s consists of lowercase English letters.


class Solution:
    def maximumGain(self, s: str, x: int, y: int) -> int:
        a = 'a'
        b = 'b'
        if x < y:
            x, y = y, x
            a, b = b, a
        seen = Counter()
        ans = 0
        for c in s + 'x':
            if c in 'ab':
                if c == b and 0 < seen[a]:
                    ans += x
                    seen[a] -= 1
                else:
                    seen[c] += 1
            else:
                ans += y * min(seen[a], seen[b])
                seen = Counter()

        return ans
class Solution {
    public int maximumGain(String s, int x, int y) {
        
        int aCount = 0;
        int bCount = 0;
        int lesser = Math.min(x, y);
        int result = 0;
        
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c > 'b') {
                result += Math.min(aCount, bCount) * lesser;
                aCount = 0;
                bCount = 0;
            } else if (c == 'a') {
                if (x < y && bCount > 0) {
                    bCount--;
                    result += y;
                } else {
                    aCount++;
                }
            } else {
                if (x > y && aCount > 0) {
                    aCount--;
                    result += x;
                } else {
                    bCount++;
                };
            }
        }
        
        result += Math.min(aCount, bCount) * lesser;
        
        return result;
    }
}
class Solution {
public:

    int helper(string&str, char a, char b){
        int count =0;
        stack<char> st;
        for(int i=0;i<str.length();i++) {
            if(!st.empty() && str[i]==b && st.top()==a) {
                st.pop();
                count++;
            }
            else {
                st.push(str[i]);
            }
        }
        str="";
        while(!st.empty()) {
            str += st.top();
              st.pop();
            }
        reverse(str.begin(),str.end());
        return count;
    }

    int maximumGain(string s, int x, int y) {
        int ca=0,cb=0;
        if(x>y) {
            ca = helper(s,'a','b');
            cb = helper(s,'b','a');
        }
        else {
            cb = helper(s,'b','a');
            ca = helper(s,'a','b');
        }
        return ca*x + cb*y;
    }
};
var maximumGain = function(s, x, y) {
    const n = s.length;
    
    let totPoints = 0;
    let stack;
    
    if (x > y) {
        stack = remove(s, x, "ab");
        s = stack.join("");
        remove(s, y, "ba");
        
    }
    else {
        stack = remove(s, y, "ba");
        s = stack.join("");
        remove(s, x, "ab");
    }
    
    return totPoints;
    
    
    function remove(str, points, match) {
        const stack = [];
        
        for (let i = 0; i < str.length; i++) {
            const char = str.charAt(i);
            
            if (stack.length > 0 && stack[stack.length - 1] + char == match) {
                totPoints += points;
                stack.pop();
            }
            else {
                stack.push(char);
            }
        }
        
        return stack;
    }
};
Maximum Score From Removing Substrings