Maximum Score After Splitting a String

Given a string s of zeros and ones, return the maximum score after splitting the string into two non-empty substrings (a left substring and a right substring). The score is the number of zeros in the left substring plus the number of ones in the right substring.

30:00

Maximum Score After Splitting a String
easy
Free written solution
Topics
Companies

Given a string s of zeros and ones, return the maximum score after splitting the string into two non-empty substrings (a left substring and a right substring). The score is the number of zeros in the left substring plus the number of ones in the right substring.

Example 1:
Input: {"s":"011101"}
Output: 5
Constraints:
  • 2≤s.length≤5002 \leq \text{s.length} \leq 500

  • ss consists of the characters '0' and '1' only.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"011101"}

Before any cut, all 4 ones sit on the right and no zeros sit on the left. Each step moves the cut one character right.

0

0

1

1

1

2

1

3

0

4

1

5

zeros on left

0

ones on right

4

score (best 0)

-

score at each cut position

Variables
VariableValue
zeros0
ones4
best0
DepthFunction Call
Stack empty
0/11