Maximum Score After Splitting a String
Real-world version: Cutting the Strip for the Best ScoreThe statement here is our own: the same inputs, outputs and technique.
A strip marks holds only the characters 0 and 1. Cut it once into a left piece and a right piece, and neither piece may be empty.
A cut scores the number of 0 characters in the left piece plus the number of 1 characters in the right piece.
Return the highest score a cut can reach.
Examples
Example 1
- Input
- marks = "0011"
- Output
- 4
Cutting after the two zeros puts both of them on the left and both ones on the right, scoring 2 plus 2.
Example 2
- Input
- marks = "10"
- Output
- 0
Only one cut is possible. The left piece is a single 1 and the right piece a single 0, so neither side scores anything.
Example 3
- Input
- marks = "0000011111"
- Output
- 10
Cutting where the zeros run out puts all five zeros on the left and all five ones on the right.
Constraints
- 2 <= marks.length <= 500
- marks holds only the characters 0 and 1
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 max_score(marks: str) -> int:public int maxScore(String marks)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
- Jump Game VIIReal-world version: Pontoon Crossing With A Hop RangeMedium
- Get Equal Substrings Within BudgetReal-world version: Longest Stretch Retinted Within BudgetMedium
- Number of Wonderful SubstringsReal-world version: Nearly Even Telemetry StretchesMedium
- Longest Balanced Substring After One SwapReal-world version: Longest Even Stretch After One SwapMedium