Maximum Score From Removing Substrings - 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.
Input & Output
Example 1 — Basic Case
$
Input:
s = "caba", x = 2, y = 1
›
Output:
2
💡 Note:
Remove the substring "ab" at positions 1-2 to get "ca", earning 2 points. No more "ab" or "ba" pairs can be formed. Total score: 2.
Example 2 — Higher Y Value
$
Input:
s = "aabb", x = 1, y = 2
›
Output:
2
💡 Note:
Since y > x, we prioritize "ba" pairs, but "aabb" contains no "ba" substrings. We can form 2 "ab" pairs for 2×1 = 2 points total.
Example 3 — No Pairs Possible
$
Input:
s = "aaa", x = 3, y = 4
›
Output:
0
💡 Note:
String contains only 'a' characters, so no "ab" or "ba" substrings can be formed. Total score: 0.
Constraints
- 1 ≤ s.length ≤ 105
- 1 ≤ x, y ≤ 104
- s consists of lowercase English letters
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code