- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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

- Related Questions & Answers
- Splitting a string into maximum parts in JavaScript
- Program to find maximum additive score by deleting numbers in Python
- Splitting a string into parts in JavaScript
- Program to find maximum score in stone game in Python
- Program to find maximum score from removing stones in Python
- Splitting number into n parts close to each other in JavaScript
- Divide number into two parts divisible by given numbers in C++ Program
- Program to find maximum score of brick removal game in Python
- Program to find maximum score from performing multiplication operations in Python
- Program to find maximum score of a good subarray in Python
- Divide a number into two parts in C++ Program
- Java Program to Add Two Binary Strings
- Divide a big number into two parts that differ by k in C++ Program
- Program to add two binary strings in C++
- Program to find maximum score we can get in jump game in Python

Advertisements