📌  相关文章
📜  检查二进制字符串是否包含一对 0 和 B 独立的 0

📅  最后修改于: 2021-09-06 11:24:08             🧑  作者: Mango

给定一个二进制字符串S和两个正整数AB,该任务是检查字符串由一个独立的一对相邻的0和二进制字符串或不0独立数目的。如果发现是真的,则打印“是” 。否则,打印“否”

例子:

处理方法:按照以下步骤解决问题:

  • 使用变量i遍历给定的字符串S 并执行以下步骤:
    • 如果当前字符为‘0’且其相邻字符为‘0’A至少为 1 ,则将A1并将指针i增加1
    • 否则,如果当前字符为‘0’B至少为 1 ,则将B1
  • 完成上述步骤后,如果AB 的值为0 ,则打印“是” 。否则,打印“否”

下面是上述方法的实现:

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


输出:
Yes

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live