# Program to find maximum score by splitting binary strings into two parts in Python

PythonServer Side ProgrammingProgramming

Suppose we have a binary string s. Now let us consider an operation, where we split the string into two non-empty substrings s1 and s2. The score of this split is the sum of "0"s count in s1 and sum of "1"s count in s2. We have to find the maximum score we can obtain.

So, if the input is like s = "011001100111", then the output will be 8, because we can split the string like "01100" + "110111". Then, the score is 3 + 5 = 8.

To solve this, we will follow these steps −

• ones := number of "1"s in s

• zeros := 0

• ans := 0

• for i in range 0 to size of s - 2, do

• if s[i] is same as "0", then

• zeros := zeros + 1

• otherwise,

• ones := ones - 1

• ans := maximum of ans and (ones + zeros)

• return ans

## Example

Let us see the following implementation to get better understanding

def solve(s):
ones = s.count("1")
zeros = 0
ans = 0
for i in range(len(s) - 1):
if s[i] == "0":
zeros += 1
else:
ones -= 1
ans = max(ans, ones + zeros)
return ans

s = "011001100111"
print(solve(s))

## Input

"011001100111"


## Output

8