You are given a string s of lowercase English letters and two integers pointsAB and pointsBA. You may repeatedly apply either operation, any number of times and in any order:
Remove one occurrence of the substring "ab" and gain pointsAB points.
Remove one occurrence of the substring "ba" and gain pointsBA points.
Removing a substring joins the characters on its two sides, which may create new removable substrings.
Return the maximum total points you can gain.
Examples
Example 1
Input: s = "cdbcbbaaabab", pointsAB = 4, pointsBA = 5
Output: 19
Explanation: Removing "ba" occurrences (5 points each) before "ab"
occurrences (4 points each) yields the maximum of 19 points.
Example 2
Input: s = "aabbaaxybbaabb", pointsAB = 5, pointsBA = 4
Output: 20
Explanation: Removing "ab" occurrences (5 points each) first is optimal here,
for a total of 20 points.
Constraints
1≤s.length≤105
1≤pointsAB, pointsBA≤104
s consists of lowercase English letters.
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up