Show problems as
Classic problem #1422EasyStringPrefix Sum

Maximum Score After Splitting a String

Real-world version: Cutting the Strip for the Best Score
Tracked in this browser only
Read Maximum Score After Splitting a String on LeetCode(opens in a new tab)

The 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.

Python
def max_score(marks: str) -> int:
Java
public int maxScore(String marks)