Split a Binary String such that count of 0s and 1s in left and right substrings is maximum
Last Updated :
31 May, 2021
Given a binary string, str of length N, the task is to find the maximum sum of the count of 0s on the left substring and count of 1s on the right substring possible by splitting the binary string into two non-empty substrings.
Examples:
Input: str = “000111”
Output: 6
Explanation:
Splitting the binary string into “000” and “111”.
Count of 0s in the left substring of the string = 3
Count of 1s in the right substring of the string = 3
Therefore, the sum of the count of 0s in the left substring of the string and the count of 1s in the right substring of the string = 3 + 3 = 6.
Input: S = “1111”
Output: 3
Explanation:
Splitting the binary string into “1” and “111”.
Count of 0s in the left substring of the string = 0
Count of 1s in the right substring of the string = 3
Therefore, the sum of the count of 0s in the left substring of the string and the count of 1s in the right substring of the string = 0 + 3 = 3.
Approach: Follow the steps below to solve the problem:
- Initialize a variable, say res, to store the maximum sum of count of 0s in left substring and count of 1s in the right substring.
- Initialize a variable, say cntOne, to store count of 1s in the given binary string.
- Traverse the binary string and for each character, check if it is ‘1’ or not. If found to be true, then increment the value of cntOne by 1.
- Initialize two variables, say zero and one, to store the count of 0s and count of 1s till ith index.
- Traverse the binary string and update the value of res = max(res, cntOne – one + zero).
- Finally, print the value of res.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxSumbySplittingstring(string str, int N)
{
int cntOne = 0;
for ( int i = 0; i < N; i++) {
if (str[i] == '1' ) {
cntOne++;
}
}
int zero = 0;
int one = 0;
int res = 0;
for ( int i = 0; i < N - 1; i++) {
if (str[i] == '0' ) {
zero++;
}
else {
one++;
}
res = max(res, zero + cntOne - one);
}
return res;
}
int main()
{
string str = "00111" ;
int N = str.length();
cout << maxSumbySplittingstring(str, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int maxSumbySplittingString(String str, int N)
{
int cntOne = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (str.charAt(i) == '1' )
{
cntOne++;
}
}
int zero = 0 ;
int one = 0 ;
int res = 0 ;
for ( int i = 0 ; i < N - 1 ; i++) {
if (str.charAt(i) == '0' ) {
zero++;
}
else {
one++;
}
res = Math.max(res, zero + cntOne - one);
}
return res;
}
public static void main(String[] args)
{
String str = "00111" ;
int N = str.length();
System.out.print(maxSumbySplittingString(str, N));
}
}
|
Python3
def maxSumbySplittingstring( str , N):
cntOne = 0
for i in range (N):
if ( str [i] = = '1' ):
cntOne + = 1
zero = 0
one = 0
res = 0
for i in range (N - 1 ):
if ( str [i] = = '0' ):
zero + = 1
else :
one + = 1
res = max (res, zero + cntOne - one)
return res
if __name__ = = '__main__' :
str = "00111"
N = len ( str )
print (maxSumbySplittingstring( str , N))
|
C#
using System;
public class GFG
{
static int maxSumbySplittingString(String str, int N)
{
int cntOne = 0;
for ( int i = 0; i < N; i++)
{
if (str[i] == '1' )
{
cntOne++;
}
}
int zero = 0;
int one = 0;
int res = 0;
for ( int i = 0; i < N - 1; i++) {
if (str[i] == '0' ) {
zero++;
}
else {
one++;
}
res = Math.Max(res, zero + cntOne - one);
}
return res;
}
public static void Main(String[] args)
{
String str = "00111" ;
int N = str.Length;
Console.Write(maxSumbySplittingString(str, N));
}
}
|
Javascript
<script>
function maxSumbySplittingstring(str, N)
{
var cntOne = 0;
for ( var i = 0; i < N; i++) {
if (str[i] == '1' ) {
cntOne++;
}
}
var zero = 0;
var one = 0;
var res = 0;
for ( var i = 0; i < N - 1; i++) {
if (str[i] == '0' ) {
zero++;
}
else {
one++;
}
res = Math.max(res, zero + cntOne - one);
}
return res;
}
var str = "00111" ;
var N = str.length;
document.write( maxSumbySplittingstring(str, N));
</script>
|
Time complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...