Maximum Score After Splitting a String - Problem

Given a string s consisting of zeros and ones only, return the maximum score after splitting the string into two non-empty substrings (left substring and right substring).

The score is calculated as:

  • Number of zeros in the left substring
  • Plus number of ones in the right substring

Both substrings must be non-empty after the split.

Input & Output

Example 1 — Basic Case
$ Input: s = "011101"
Output: 5
💡 Note: Split after index 0: left="0" (1 zero), right="11101" (4 ones), score = 1+4 = 5. This is the maximum possible.
Example 2 — All Zeros
$ Input: s = "00"
Output: 1
💡 Note: Only one valid split: left="0" (1 zero), right="0" (0 ones), score = 1+0 = 1.
Example 3 — Mixed String
$ Input: s = "1111"
Output: 3
💡 Note: Best split after index 0: left="1" (0 zeros), right="111" (3 ones), score = 0+3 = 3.

Constraints

  • 2 ≤ s.length ≤ 500
  • The string s consists of zeros and ones only

Visualization

Tap to expand
Maximum Score After Splitting a StringFind the split that maximizes: zeros_left + ones_right011101Split hereLeft SideRight Side"0""11101"1 zero4 onesMaximum Score: 1 + 4 = 5
Understanding the Visualization
1
Input
Binary string that needs to be split into two non-empty parts
2
Split
Choose split position to maximize zeros left + ones right
3
Score
Count zeros in left substring plus ones in right substring
Key Takeaway
🎯 Key Insight: Use running counts to avoid recalculating at each split position
Asked in
Google 15 Amazon 12 Microsoft 8
28.0K Views
Medium Frequency
~15 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen