Check if count of 1s can be made greater in a Binary string by changing 0s adjacent to 1s
Last Updated :
24 Nov, 2022
Given a binary string S of size N, the task is to check if the count of 1s can be made greater than the count of 0s by changing the 0s adjacent to 1s to any other characters. If it is possible, then print Yes. Otherwise, print No.
Note: Any index having 1 can be chosen at most once.
Examples:
Input: S = “01”
Output: Yes
Explanation:
Choose ‘1’ at index 1 and change its left adjacent 0 to ‘_’, modifies the string to “_1”.
Now, the count of 1s is 1 which is greater than the count of 0’s i.e., 0. Therefore, print Yes.
Input: S = “001110000”
Output: No
Approach: The given problem can be solved by counting the number of 1s and 0s in the string S and then while traversing the string if any index i at which the value is ‘1’ and if left-side or right-side(Not Both) is ‘0’ then change it to ‘_’. The value is changed to ‘_’ and not ‘1’ so that it is not used again. After changing the value, decrease the count of 0’s by 1. Follow the steps below to solve the problem:
- Initialize two variables, say cnt0 and cnt1 as 0 to store the count of 0s and 1s.
- Traverse the string S and store the count of 1s and 0s in the variables cnt0 and cnt1 respectively.
- Traverse the string S from the left side and check current character is 1 if found to be true then check for the condition:
- if(i > 0 && S[i – 1] == ‘0’) if it is found to be true then change the left adjacent 0 to S[i-1] = ‘_’ and decrement the value of cnt0 by 1.
- else if(i < S.length() && S[i+1] == ‘0’) if it is found to be true then change the right 0 to S[i+1] = ‘_’ and decrement the value of cnt0 by 1.
- After completing the above steps, if the value of (cnt1 > cnt0), then print “Yes”. Otherwise, print “No”.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void isOnesGreater(string S, int N)
{
int cnt0 = 0;
int cnt1 = 0;
for ( int i = 0; i < N; i++) {
if (S[i] == '1' )
cnt1++;
else
cnt0++;
}
for ( int i = 0; i < N; i++) {
if (S[i] == '1' ) {
if (i > 0 && S[i - 1] == '0' ) {
S[i - 1] = '_' ;
cnt0--;
}
else if (i < N && S[i + 1] == '0' ) {
S[i + 1] = '_' ;
cnt0--;
}
}
}
if (cnt1 > cnt0) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
}
int main()
{
string S = "01" ;
int N = S.length();
isOnesGreater(S, N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void isOnesGreater(String S, int N)
{
char [] st = new char [S.length()];
for ( int i = 0 ; i < S.length(); i++) {
st[i] = S.charAt(i);
}
int cnt0 = 0 ;
int cnt1 = 0 ;
for ( int i = 0 ; i < N; i++) {
if (st[i] == '1' )
cnt1++;
else
cnt0++;
}
for ( int i = 0 ; i < N; i++) {
if (st[i] == '1' ) {
if (i > 0 && st[i - 1 ] == '0' ) {
st[i - 1 ] = '_' ;
cnt0--;
}
else if (i < N && st[i + 1 ] == '0' ) {
st[i + 1 ] = '_' ;
cnt0--;
}
}
}
if (cnt1 > cnt0) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
public static void main(String args[])
{
String S = "01" ;
int N = S.length();
isOnesGreater(S, N);
}
}
|
Python3
def isOnesGreater(S, N):
S = list (S)
cnt0 = 0
cnt1 = 0
for i in range (N):
if (S[i] = = '1' ):
cnt1 + = 1
else :
cnt0 + = 1
for i in range (N):
if (S[i] = = '1' ):
if (i > 0 and S[i - 1 ] = = '0' ):
S[i - 1 ] = '_'
cnt0 - = 1
elif (i < N and S[i + 1 ] = = '0' ):
S[i + 1 ] = '_'
cnt0 - = 1
if (cnt1 > cnt0):
print ( "Yes" )
else :
print ( "No" )
S = "01"
N = len (S)
isOnesGreater(S, N)
|
C#
using System;
using System.Text;
class GFG {
static void isOnesGreater( string S, int N)
{
StringBuilder st = new StringBuilder(S);
int cnt0 = 0;
int cnt1 = 0;
for ( int i = 0; i < N; i++) {
if (st[i] == '1' )
cnt1++;
else
cnt0++;
}
for ( int i = 0; i < N; i++) {
if (st[i] == '1' ) {
if (i > 0 && st[i - 1] == '0' ) {
st[i - 1] = '_' ;
cnt0--;
}
else if (i < N && st[i + 1] == '0' ) {
st[i + 1] = '_' ;
cnt0--;
}
}
}
if (cnt1 > cnt0) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "No" );
}
}
public static void Main()
{
string S = "01" ;
int N = S.Length;
isOnesGreater(S, N);
}
}
|
Javascript
<script>
function isOnesGreater(S, N) {
let cnt0 = 0;
let cnt1 = 0;
for (let i = 0; i < N; i++) {
if (S[i] == '1' )
cnt1++;
else
cnt0++;
}
for (let i = 0; i < N; i++) {
if (S[i] == '1' ) {
if (i > 0 && S[i - 1] == '0' ) {
S[i - 1] = '_' ;
cnt0--;
}
else if (i < N && S[i + 1] == '0' ) {
S[i + 1] = '_' ;
cnt0--;
}
}
}
if (cnt1 > cnt0) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
}
let S = "01" ;
let N = S.length;
isOnesGreater(S, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...