
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 Articles
- Splitting a string into maximum parts in JavaScript
- Program to find maximum additive score by deleting numbers in Python
- Program to find maximum score in stone game in Python
- Splitting a string into parts in JavaScript
- Program to find maximum score from removing stones in Python
- Program to find maximum score from performing multiplication operations in Python
- Program to find maximum score of brick removal game in Python
- Program to find maximum score of a good subarray in Python
- Splitting number into n parts close to each other in JavaScript
- Program to find maximum score we can get in jump game in Python
- Program to add two binary strings in C++
- Java Program to Add Two Binary Strings
- Program to find the maximum score from all possible valid paths in Python
- Program to find maximum binary string after change in python
- Program to get maximum length merge of two given strings in Python

Advertisements