> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Maximum Score After Splitting a String

> Tested Python solution for LeetCode 1422 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 1422, [Easy](/catalog/easy). Topics: [String](/catalog/topics/string), [Prefix Sum](/catalog/topics/prefix-sum). [View on LeetCode](https://leetcode.com/problems/maximum-score-after-splitting-a-string/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1422   # by problem number
lcpy gen -s maximum_score_after_splitting_a_string   # by problem name
```

## Problem

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

The score after splitting a string is the number of **zeros** in the **left** substring plus the number of **ones** in the **right** substring.

### Examples

```
Input: s = "011101"
Output: 5
```

**Explanation:** All possible ways of splitting `s` into two non-empty substrings are: `left = "0"` and `right = "11101"`, score = 1 + 4 = 5; `left = "01"` and `right = "1101"`, score = 1 + 3 = 4; `left = "011"` and `right = "101"`, score = 1 + 2 = 3; `left = "0111"` and `right = "01"`, score = 1 + 1 = 2; `left = "01110"` and `right = "1"`, score = 2 + 1 = 3.

```
Input: s = "00111"
Output: 5
```

**Explanation:** When `left = "00"` and `right = "111"`, we get the maximum score = 2 + 3 = 5.

```
Input: s = "1111"
Output: 3
```

### Constraints

* `2 <= s.length <= 500`
* The string `s` consists of characters `'0'` and `'1'` only.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_score_after_splitting_a_string/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_score_after_splitting_a_string/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def max_score(self, s: str) -> int:
        left_zeros = 1 if s[0] == "0" else 0
        right_ones = s[1:].count("1")
        best = left_zeros + right_ones
        for ch in s[1:-1]:
            if ch == "0":
                left_zeros += 1
            else:
                right_ones -= 1
            best = max(best, left_zeros + right_ones)
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.