> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Maximum Score From Removing Substrings

> Tested Python solution for LeetCode 1717 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 1717, [Medium](/catalog/medium). Topics: [String](/catalog/topics/string), [Stack](/catalog/topics/stack), [Greedy](/catalog/topics/greedy). [View on LeetCode](https://leetcode.com/problems/maximum-score-from-removing-substrings/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1717   # by problem number
lcpy gen -s maximum_score_from_removing_substrings   # by problem name
```

## Problem

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

### Examples

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

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

### Constraints

* 1 \<= s.length \<= 10^5
* 1 \<= x, y \<= 10^4
* s consists of lowercase English letters.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_score_from_removing_substrings/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_score_from_removing_substrings/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def maximum_gain(self, s: str, x: int, y: int) -> int:
        hi_first, hi_second, hi = "a", "b", x
        lo_first, lo_second, lo = "b", "a", y
        if x < y:
            hi_first, hi_second, hi = "b", "a", y
            lo_first, lo_second, lo = "a", "b", x

        total = 0
        stack: list[str] = []
        for ch in s:
            if stack and stack[-1] == hi_first and ch == hi_second:
                stack.pop()
                total += hi
            else:
                stack.append(ch)

        leftover: list[str] = []
        for ch in stack:
            if leftover and leftover[-1] == lo_first and ch == lo_second:
                leftover.pop()
                total += lo
            else:
                leftover.append(ch)
        return total
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.