Maximum Score From Removing Substrings

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.

Example 1
Inputs = "cdbcbbaaabab", x = 4, y = 5
Output19
Removing substrings in an optimal order gives a total score of 5 + 4 + 5 + 5 = 19.
Example 2
Inputs = "aabbaaxybbaabb", x = 5, y = 4
Output20
The maximum score obtainable by removing "ab" and "ba" substrings is 20.

Constraints

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

Asked at 7 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate