Maximum Score From Removing Substrings
Real-world version: Pulling Pairs Off the TapeThe statement here is our own: the same inputs, outputs and technique.
A tape tape holds lowercase letters. Two moves are allowed, each usable as often as the pattern shows up:
- Pull out a neighbouring pair reading
aband collectxpoints. - Pull out a neighbouring pair reading
baand collectypoints.
Pulling a pair out closes the gap, so letters that were apart may become neighbours.
Return the most points that can be collected.
Examples
Example 1
- Input
- tape = "aba", x = 1, y = 5
- Output
- 5
Only one pull is ever possible. Taking ba from the back pays 5, while taking ab from the front would pay 1.
Example 2
- Input
- tape = "bbba", x = 5, y = 1
- Output
- 1
The only pair on offer reads ba and pays 1. Once it is gone the tape reads bb with nothing left to pull.
Example 3
- Input
- tape = "abcba", x = 5, y = 1
- Output
- 6
The c splits the tape in two. On the left ab pays 5 and on the right ba pays 1.
Constraints
- 1 <= tape.length <= 10^5
- 1 <= x <= 10^4
- 1 <= y <= 10^4
- tape holds only lowercase English letters
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def maximum_gain(tape: str, x: int, y: int) -> int:public int maximumGain(String tape, int x, int y)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Valid Parenthesis StringReal-world version: Salvage a Smudged Span TraceMedium
- Remove K DigitsReal-world version: Trim the Freight CodeMedium
- Using a Robot to Print the Lexicographically Smallest StringReal-world version: Typesetter's RackMedium
- Smallest Subsequence of Distinct CharactersReal-world version: Earliest Depot Summary TagMedium