<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:
    def maximumGain(self, s: str, x: int, y: int) -> int:
        # Define the two pairs of substrings and their corresponding scores
        if x > y:
            first_pair, first_score = "ab", x
            second_pair, second_score = "ba", y
        else:
            first_pair, first_score = "ba", y
            second_pair, second_score = "ab", x
        
        # Helper function to process a given pair and return score
        def process(stack, pair, score):
            result_stack = []
            local_score = 0
            for char in stack:
                if result_stack and result_stack[-1] == pair[0] and char == pair[1]:
                    result_stack.pop()  # Remove the pair
                    local_score += score
                else:
                    result_stack.append(char)
            return result_stack, local_score
        
        # Step 1: Process the first pair
        stack, total_score = process(s, first_pair, first_score)
        
        # Step 2: Process the second pair on the remaining stack
        _, second_score_gained = process(stack, second_pair, second_score)
        total_score += second_score_gained
        
        return total_score

    """
    def find_all_occurrences(self, string, substring):
        indices = []
        index = string.find(substring)
        
        while index != -1:
            indices.append(index)
            index = string.find(substring, index + 1)
        
        return indices

    def maximumGain(self, s: str, x: int, y: int) -> int:
        if s in self.occurrences:
            return self.occurrences[s]

        max_gain = 0
        
        indices = self.find_all_occurrences(s, "ab")
        for idx in indices:
            removed_str = s[:idx]
            if idx < len(s):
                removed_str += s[idx + 2:]
            curr_max_gain = x + self.maximumGain(removed_str, x, y)
            max_gain = max(max_gain, curr_max_gain)

        indices = self.find_all_occurrences(s, "ba")
        for idx in indices:
            removed_str = s[:idx]
            if idx < len(s):
                removed_str += s[idx + 2:]
            curr_max_gain = y + self.maximumGain(removed_str, x, y)
            max_gain = max(max_gain, curr_max_gain)
        
        self.occurrences[s] = max_gain

        return max_gain
    """
260