# Check if a Binary String contains A pairs of 0s and B independent 0s or not

• Last Updated : 27 Apr, 2021

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:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to check if there exists``// A adjacent 0s and B 0s in the``// given binary string or not``void` `parking(string S, ``int` `a, ``int` `b)``{``    ``// Traverse the string``    ``for` `(``int` `i = 0; i < S.size(); i++) {``        ``if` `(S[i] == ``'0'``) {``            ``// If there are adjacent``            ``// 0s and a is positive``            ``if` `(i + 1 < S.size()``                ``&& S[i + 1] == ``'0'``                ``&& a > 0) {` `                ``i++;``                ``a--;``            ``}` `            ``// If b is positive``            ``else` `if` `(b > 0) {``                ``b--;``            ``}``        ``}``    ``}` `    ``// Condition for Yes``    ``if` `(a == 0 && b == 0) {``        ``cout << ``"Yes\n"``;``    ``}``    ``else``        ``cout << ``"No\n"``;``}` `// Driver Code``int` `main()``{``    ``string S = ``"10100"``;``    ``int` `A = 1, B = 1;``    ``parking(S, A, B);``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG{` `// Function to check if there exists``// A adjacent 0s and B 0s in the``// given binary string or not``static` `void` `parking(String S, ``int` `a, ``int` `b)``{``    ` `    ``// Traverse the string``    ``for``(``int` `i = ``0``; i < S.length(); i++)``    ``{``        ``if` `(S.charAt(i) == ``'0'``)``        ``{``            ` `            ``// If there are adjacent``            ``// 0s and a is positive``            ``if` `(i + ``1` `< S.length() &&``                ``S.charAt(i + ``1``) == ``'0'` `&& a > ``0``)``            ``{``                ``i++;``                ``a--;``            ``}` `            ``// If b is positive``            ``else` `if` `(b > ``0``)``            ``{``                ``b--;``            ``}``        ``}``    ``}` `    ``// Condition for Yes``    ``if` `(a == ``0` `&& b == ``0``)``    ``{``        ``System.out.print(``"Yes\n"``);``    ``}``    ``else``        ``System.out.print(``"No\n"``);``}` `// Driver Code``public` `static` `void` `main (String[] args)``{``    ` `    ``// Given string``    ``String S = ``"10100"``;``    ``int` `A = ``1``, B = ``1``;``    ` `    ``parking(S, A, B);``}``}` `// This code is contributed by sanjoy_62`

## Python3

 `# Python3 program for the above approach` `# Function to check if there exists``# A adjacent 0s and B 0s in the``# given binary string or not``def` `parking(S, a, b):``    ` `    ``# Traverse the string``    ``for` `i ``in` `range``(``len``(S)):``        ``if` `(S[i] ``=``=` `'0'``):``            ` `            ``# If there are adjacent``            ``# 0s and a is positive``            ``if` `(i ``+` `1` `< ``len``(S) ``and``              ``S[i ``+` `1``] ``=``=` `'0'` `and` `a > ``0``):``                ``i ``+``=` `1``                ``a ``-``=` `1` `            ``# If b is positive``            ``elif` `(b > ``0``):``                ``b ``-``=` `1` `    ``# Condition for Yes``    ``if` `(a ``=``=` `0` `and` `b ``=``=` `0``):``        ``print``(``"Yes"``)``    ``else``:``        ``print``(``"No"``)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``S ``=` `"10100"``    ``A ``=` `1``    ``B ``=` `1``    ` `    ``parking(S, A, B)``    ` `# This code is contributed by SURENDRA_GANGWAR`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{` `// Function to check if there exists``// A adjacent 0s and B 0s in the``// given binary string or not``static` `void` `parking(``string` `S, ``int` `a, ``int` `b)``{``    ` `    ``// Traverse the string``    ``for``(``int` `i = 0; i < S.Length; i++)``    ``{``        ``if` `(S[i] == ``'0'``)``        ``{``            ` `            ``// If there are adjacent``            ``// 0s and a is positive``            ``if` `(i + 1 < S.Length &&``                 ``S[i + 1] == ``'0'` `&& a > 0)``            ``{``                ``i++;``                ``a--;``            ``}` `            ``// If b is positive``            ``else` `if` `(b > 0)``            ``{``                ``b--;``            ``}``        ``}``    ``}` `    ``// Condition for Yes``    ``if` `(a == 0 && b == 0)``    ``{``        ``Console.WriteLine(``"Yes"``);``    ``}``    ``else``         ``Console.WriteLine(``"No"``);``}` `// Driver Code``public` `static` `void` `Main (``string``[] args)``{``    ` `    ``// Given string``    ``string` `S = ``"10100"``;``    ``int` `A = 1, B = 1;``    ` `    ``parking(S, A, B);``}``}` `// This code is contributed by AnkThon`

## Javascript

 ``
Output:
`Yes`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up