Problem statement

https://binarysearch.com/problems/Maximize-Binary-String-Score/

Solution

Equal to Leetcode 1422. Maximum Score After Splitting a String.

Complexity

It is O(n) for time and space.

Code

class Solution:
    def solve(self, s):
        n, ans = len(s), 0
        acc = [0] + list(accumulate([int(i) for i in s]))
        for i in range(1, n):
            ans = max(ans, i - 2*acc[i] + acc[-1])
        return ans