Maximum Score From Removing Substrings
You receive a string s and two integers x and y. You can perform two types of operations any number of times.
-
Remove substring
"ab"and gainxpoints.- For example, when removing
"ab"from"c ab xbae"it becomes"cxbae".
- For example, when removing
-
Remove substring
"ba"and gainypoints.- For example, when removing
"ba"from"cabx ba e"it becomes"cabxe".
- For example, when removing
Produce the maximum points you can gain after applying the above operations on s.
Input: s = "cdbcbbaaabab", x = 4, y = 5
Output: 19
Explanation:
-
Remove the "ba" highlighted in "cdbcbbaaabab". Now, s = "cdbcbbaaab" and 5 points are added to the score.
-
Remove the "ab" highlighted in "cdbcbbaaab". Now, s = "cdbcbbaa" and 4 points are added to the score.
-
Remove the "ba" highlighted in "cdbcbbaa". Now, s = "cdbcba" and 5 points are added to the score.
-
Remove the "ba" highlighted 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^51 <= x, y <= 10^4sconsists of lowercase English letters.
Did this problem appear in one of your interviews?
Responses stay private.
