Ctrl K

Points for Deleting "ab" and "ba"

Medium
You are given a string text and two numbers ab_points and ba_points. As many times as you like, you may do either of these: - delete a substring "ab" (two adjacent letters) and earn ab_points points; - delete a substring "ba" and earn ba_points points. After a deletion the remaining parts are joined together, which may create new pairs. Return the most points you can earn.
Example 1
text='xbabyab', ab_points=3, ba_points=1 → 6

In "bab" you can delete either "ba" or "ab"; take "ab" for 3 points. Then delete the trailing "ab" for 3 more. Total 6.

Example 2
text='bbaa', ab_points=10, ba_points=1 → 2

There is no "ab" here. Delete the middle "ba", which leaves "ba", and delete that too: 1 + 1 = 2.

Constraints
  • 1 ≤ text.length ≤ 10⁵
  • text consists of lowercase English letters
  • 1 ≤ ab_points, ba_points ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsEvery Letter Once MediumErase Digits for the Smallest Number MediumSmallest Up-Down Sequence MediumBrackets With Wildcards MediumBrackets to Patch In Medium
loading Python…
Input
text='xbabyab', ab_points=3, ba_points=1
Expected
6

Run checks the first 3 tests in your browser. Submit checks all 153 tests on the server, hidden ones included.