<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>

import re

class Solution:
    # known = {}
    def maximumGain(self, s: str, x: int, y: int) -> int:
        x_pattern = r'^(a*)(b*)$'
        y_pattern = r'^(b*)(a*)$'
        match = re.match(x_pattern, s)
        if match:
            # Extract the first group, which is the sequence of 'a's
            a_sequence = match.group(1)
            b_sequence = match.group(2)
            return x*min(len(a_sequence), len(b_sequence))
        match = re.match(y_pattern, s)
        if match:
            # Extract the first group, which is the sequence of 'a's
            b_sequence = match.group(1)
            a_sequence = match.group(2)
            return y*min(len(a_sequence), len(b_sequence))

        pattern = r'^(a+)(b+)(a+)$'
        match = re.fullmatch(pattern, s)
        if match:
            if y > x:
                if len(match.group(2)) <= len(match.group(3)):
                    return y* len(match.group(2))
                else:
                    return y*len(match.group(3)) + x* min(len(match.group(1)), len(match.group(2)) - len(match.group(3)))
            else:
                if len(match.group(2)) <= len(match.group(1)):
                    return x* len(match.group(2))
                else:
                    return x*len(match.group(1)) + y* min(len(match.group(3)), len(match.group(2)) - len(match.group(1)))
            pattern = r'^(b+)(a+)(b+)$'
            match = re.fullmatch(pattern, s)
            if match:
                if y > x:
                    if len(match.group(2)) <= len(match.group(1)):
                        return y* len(match.group(2))
                    else:
                        return y*len(match.group(1)) + x* min(len(match.group(3)), len(match.group(2)) - len(match.group(1)))
                else:
                    if len(match.group(2)) <= len(match.group(3)):
                        return x* len(match.group(2))
                    else:
                        return x*len(match.group(3)) + y * min(len(match.group(1)), len(match.group(2)) - len(match.group(3)))
        def ab_gain(s,x,y, pos):
            if len(s) < 2:
                return 0
            if pos < 0:
                pos = 0
            if x>y:
                pattern = r'(ab)+'
                match = re.search(pattern, s[pos:])
                if match:
                    return x * (match.end() - match.start()) // 2 + ab_gain(s[:pos+match.start()] + s[pos+match.end():],x,y,match.start()-1)
            else:
                pattern = r'(ba)+'
                match = re.search(pattern, s[pos:])
                if match:
                    return y * (match.end() - match.start()) // 2 + ab_gain(s[:pos+match.start()] + s[pos+match.end():],x,y,match.start()-1)

            scores = [0]
            for k in range(0,len(s) - 1):
                if s[k:k+2] == 'ab':
                    scores.append(ab_gain(s[:k] + s[k+2:],x,y, k - 1) + x)
                if s[k:k+2] == 'ba':
                    scores.append(ab_gain(s[:k] + s[k+2:],x,y, k - 1) + y)
            return max(scores)

        if len(s) < 2:
            return 0  
        
        i = 0
        score = 0
        for j in range(len(s)):
            if not s[j] in ['a', 'b']:
                print(s[i:j])
                score += ab_gain(s[i:j],x,y,0)
                print(score)
                i = j + 1

        score += ab_gain(s[i:],x,y,0)

        return score
        
        
            
        
914