Check if a Binary String contains A pairs of 0s and B independent 0s or not
Last Updated :
19 Sep, 2023
Given a binary string S and two positive integers A and B, the task is to check if the string consists of A independent pair of adjacent 0s and B independent number of 0s in the binary string or not. If found to be true, then print “Yes”. Otherwise, print “No”.
Examples:
Input: S = “10100”, A = 1, B = 1
Output: Yes
Explanation:
The given string consists of A (=1) pairs of adjacent 0s and B (=1) independent number of 0s.
Input: S = “0101010”, A = 1, B = 2
Output: No
Explanation:
The given string has no pair of adjacent 0s.
Approach: Follow the steps below to solve the problem:
- Traverse the given string S using a variable, say i, and perform the following steps:
- If the current character is ‘0’ and its adjacent character is ‘0’ and A is at least 1, then decrease A by 1 and increase the pointer i by 1.
- Otherwise, if the current character is ‘0’ and B is at least 1, then decrease B by 1.
- After completing the above steps, if the value of A and B is 0, then print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void parking(string S, int a, int b)
{
for ( int i = 0; i < S.size(); i++) {
if (S[i] == '0' ) {
if (i + 1 < S.size()
&& S[i + 1] == '0'
&& a > 0) {
i++;
a--;
}
else if (b > 0) {
b--;
}
}
}
if (a == 0 && b == 0) {
cout << "Yes\n" ;
}
else
cout << "No\n" ;
}
int main()
{
string S = "10100" ;
int A = 1, B = 1;
parking(S, A, B);
}
|
Java
import java.util.*;
class GFG{
static void parking(String S, int a, int b)
{
for ( int i = 0 ; i < S.length(); i++)
{
if (S.charAt(i) == '0' )
{
if (i + 1 < S.length() &&
S.charAt(i + 1 ) == '0' && a > 0 )
{
i++;
a--;
}
else if (b > 0 )
{
b--;
}
}
}
if (a == 0 && b == 0 )
{
System.out.print( "Yes\n" );
}
else
System.out.print( "No\n" );
}
public static void main (String[] args)
{
String S = "10100" ;
int A = 1 , B = 1 ;
parking(S, A, B);
}
}
|
Python3
def parking(S, a, b):
for i in range ( len (S)):
if (S[i] = = '0' ):
if (i + 1 < len (S) and
S[i + 1 ] = = '0' and a > 0 ):
i + = 1
a - = 1
elif (b > 0 ):
b - = 1
if (a = = 0 and b = = 0 ):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
S = "10100"
A = 1
B = 1
parking(S, A, B)
|
C#
using System;
class GFG{
static void parking( string S, int a, int b)
{
for ( int i = 0; i < S.Length; i++)
{
if (S[i] == '0' )
{
if (i + 1 < S.Length &&
S[i + 1] == '0' && a > 0)
{
i++;
a--;
}
else if (b > 0)
{
b--;
}
}
}
if (a == 0 && b == 0)
{
Console.WriteLine( "Yes" );
}
else
Console.WriteLine( "No" );
}
public static void Main ( string [] args)
{
string S = "10100" ;
int A = 1, B = 1;
parking(S, A, B);
}
}
|
Javascript
<script>
function parking( S, a, b)
{
for ( var i = 0; i < S.length; i++) {
if (S[i] == '0' ) {
if (i + 1 < S.length
&& S[i + 1] == '0'
&& a > 0) {
i++;
a--;
}
else if (b > 0) {
b--;
}
}
}
if (a == 0 && b == 0) {
document.write( "Yes" + "<br>" );
}
else
document.write( "No" + "<br>" );
}
var S = "10100" ;
var A = 1, B = 1;
parking(S, A, B);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Approach:
We can count the number of pairs of adjacent 0s and the number of independent 0s in a single traversal of the string. While traversing the string, we maintain a count of the number of consecutive 0s encountered so far. Whenever we encounter a 1 or reach the end of the string, we update the counts of pairs and independent 0s based on the current count of consecutive 0s. Specifically, if the current count is 2 or more, we increment the count of pairs, and if the count is 1, we increment the count of independent 0s. Finally, we check if the counts match the required values.
- Initialize three variables count_pairs, count_independent, and count_consecutive to 0.
- Traverse the input string S from left to right.
- If the current character is ‘0’, increment the variable count_consecutive by 1.
- If the current character is ‘1’, update the counts of pairs and independent 0s based on the current count of consecutive 0s.
- If count_consecutive is greater than or equal to 2, increment count_pairs by 1.
- If count_consecutive is equal to 1, increment count_independent by 1.
- Reset count_consecutive to 0.
- After the traversal is complete, update the counts based on the final value of count_consecutive, since the traversal ends before updating the counts for the last group of consecutive 0s.
- If the final counts of pairs and independent 0s match the required values A and B, respectively, print “Yes”. Otherwise, print “No”.
C++
#include <iostream>
#include <string>
using namespace std;
void check_string(string S, int A, int B) {
int count_pairs = 0, count_independent = 0, count_consecutive = 0;
for ( int i = 0; i < S.length(); i++) {
if (S[i] == '0' ) {
count_consecutive++;
} else {
if (count_consecutive >= 2) {
count_pairs++;
} else if (count_consecutive == 1) {
count_independent++;
}
count_consecutive = 0;
}
}
if (count_consecutive >= 2) {
count_pairs++;
} else if (count_consecutive == 1) {
count_independent++;
}
if (count_pairs == A && count_independent == B) {
cout << "Yes\n" ;
} else {
cout << "No\n" ;
}
}
int main() {
string S = "10100" ;
int A = 1, B = 1;
check_string(S, A, B);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
String S = "10100" ;
int A = 1 , B = 1 ;
checkString(S, A, B);
}
public static void checkString(String S, int A, int B) {
int countPairs = 0 , countIndependent = 0 , countConsecutive = 0 ;
for ( int i = 0 ; i < S.length(); i++) {
if (S.charAt(i) == '0' ) {
countConsecutive++;
} else {
if (countConsecutive >= 2 ) {
countPairs++;
} else if (countConsecutive == 1 ) {
countIndependent++;
}
countConsecutive = 0 ;
}
}
if (countConsecutive >= 2 ) {
countPairs++;
} else if (countConsecutive == 1 ) {
countIndependent++;
}
if (countPairs == A && countIndependent == B) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
}
|
Python3
def check_string(S, A, B):
count_pairs = 0
count_independent = 0
count_consecutive = 0
for i in range ( len (S)):
if S[i] = = '0' :
count_consecutive + = 1
else :
if count_consecutive > = 2 :
count_pairs + = 1
elif count_consecutive = = 1 :
count_independent + = 1
count_consecutive = 0
if count_consecutive > = 2 :
count_pairs + = 1
elif count_consecutive = = 1 :
count_independent + = 1
if count_pairs = = A and count_independent = = B:
print ( "Yes" )
else :
print ( "No" )
if __name__ = = "__main__" :
S = "10100"
A = 1
B = 1
check_string(S, A, B)
|
C#
using System;
public class GFG
{
public static void CheckString( string S, int A, int B)
{
int countPairs = 0, countIndependent = 0, countConsecutive = 0;
for ( int i = 0; i < S.Length; i++)
{
if (S[i] == '0' )
{
countConsecutive++;
}
else
{
if (countConsecutive >= 2)
{
countPairs++;
}
else if (countConsecutive == 1)
{
countIndependent++;
}
countConsecutive = 0;
}
}
if (countConsecutive >= 2)
{
countPairs++;
}
else if (countConsecutive == 1)
{
countIndependent++;
}
if (countPairs == A && countIndependent == B)
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
public static void Main()
{
string S = "10100" ;
int A = 1, B = 1;
CheckString(S, A, B);
Console.ReadLine();
}
}
|
Javascript
function checkString(S, A, B) {
let countPairs = 0, countIndependent = 0, countConsecutive = 0;
for (let i = 0; i < S.length; i++) {
if (S[i] === '0' ) {
countConsecutive++;
} else {
if (countConsecutive >= 2) {
countPairs++;
} else if (countConsecutive === 1) {
countIndependent++;
}
countConsecutive = 0;
}
}
if (countConsecutive >= 2) {
countPairs++;
} else if (countConsecutive === 1) {
countIndependent++;
}
if (countPairs === A && countIndependent === B) {
console.log( "Yes" );
} else {
console.log( "No" );
}
}
const S = "10100" ;
const A = 1, B = 1;
checkString(S, A, B);
|
Time Complexity: O(n), where n is the length of the input string.
Space Complexity: O(1), since we only use a constant amount of extra memory to store the counts.
Similar Reads
Check if all the 1's in a binary string are equidistant or not
Given a binary string str, the task is to check if all the 1's in the string are equidistant or not. The term equidistant means that the distance between every two adjacent 1's is same. Note that the string contains at least two 1's. Examples: Input: str = "00111000" Output: Yes The distance between
5 min read
Check if a Binary String can be converted to another by reversing substrings consisting of even number of 1s
Given two binary strings A and B of length N, the task is to check if the string A can be converted to B by reversing substrings of A which contains even number of 1s. Examples: Input: A = "10011", B = "11100"Output: YesExplanation: Reverse substring A[2, 5], 10011 â 11100.After completing the above
9 min read
Check if Decimal representation of given Binary String is divisible by K or not
Given a binary string S, the task is to find that the decimal representation of the given binary string is divisible by integer K or not. Examples: Input: S = 1010, k = 5Output: YesExplanation: Decimal representation of 1010 (=10) is divisible by 5 Input: S = 1010, k = 6Output: No Approach: Since th
6 min read
Check if a Binary String can be sorted in decreasing order by removing non-adjacent characters
Given a binary string S of size N, the task is to check if the binary string S can be sorted in decreasing order by removing any number of the non-adjacent characters. If it is possible to sort the string in decreasing order, then print "Yes". Otherwise, print "No". Examples: Input: S = â10101011011
7 min read
Check if a string has m consecutive 1's or 0's
Given a binary string and a number m, the task is to check if the string has m consecutive 1's or 0's. Examples: Input : str = "001001", m = 2 Output : YES Input : str = "1000000001", m = 10 Output : NO The approach is to count the consecutive 1's or 0's by traversing the binary string. While traver
5 min read
C++ Program to Check String is Containing Only Digits
Prerequisite: Strings in C++ The string is the collection of characters or text in a string variable, surrounded by double quotes. One question arises How can we check string contains only digits in C++? So, to solve this query we can use the methods mentioned in the article given below: Example: "1
3 min read
Check If every group of a's is followed by a group of b's of same length
Given string str, the task is to check whether every group of consecutive a's is followed by a group of consecutive b's of the same length. If the condition is true for every group then print 1 else print 0. Examples: Input: str = "ababaabb" Output: 1 ab, ab, aabb. All groups are valid Input: str =
5 min read
Minimize count of flips required such that no substring of 0s have length exceeding K
Given a binary string str of length N and an integer K where K is in the range (1 ? K ? N), the task is to find the minimum number of flips( conversion of 0s to 1 or vice versa) required to be performed on the given string such that the resulting string does not contain K or more zeros together. Exa
6 min read
Program to build DFA that accepts the languages ending with "01" over the characters {0, 1}
Given a binary string str, the task is to build a DFA that accepts the languages ending with "01" over the characters {0, 1}. Examples: Input: str = "00011101" Output: String Accepted Explanation: As the given string ends with "01", therefore, it is accepted. Input: str = "00001111" Output: String R
9 min read
Check if all substrings of length K of a Binary String has equal count of 0s and 1s
Given a binary string S of length N and an even integer K, the task is to check if all substrings of length K contains an equal number of 0s and 1s. If found to be true, print âYesâ. Otherwise, print âNoâ. Examples: Input: S = "101010", K = 2Output: YesExplanation:Since all the substrings of length
6 min read