给定一个二进制字符串S和两个正整数A和B,该任务是检查字符串由一个独立的一对相邻的0和二进制字符串或不0的乙独立数目的。如果发现是真的,则打印“是” 。否则,打印“否” 。
例子:
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.
处理方法:按照以下步骤解决问题:
- 使用变量i遍历给定的字符串S ,并执行以下步骤:
- 如果当前字符为‘0’且其相邻字符为‘0’且A至少为 1 ,则将A减1并将指针i增加1 。
- 否则,如果当前字符为‘0’且B至少为 1 ,则将B减1 。
- 完成上述步骤后,如果A和B 的值为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