给定一个二进制字符串和一个数字m,任务是检查字符串是否具有m个连续的1或0。
例子:
Input : str = “001001”, m = 2
Output : YES
Input : str = “1000000001”, m = 10
Output : NO
该方法是通过遍历二进制字符串来计算连续的1或0。在遍历二进制字符串,请对连续出现的1或0的数量进行计数。如果有M个连续的1或0,则返回True,否则返回False 。
下面给出的是上述方法的实现:
C++
// Program to check if the binary string
// contains m consecutive 1's or 0's
#include
#include
using namespace std;
// Function that checks if
// the binary string contains m
// consecutive 1's or 0's
bool check(string s, int m)
{
// length of binary string
int l = s.length();
// counts zeros
int c1 = 0;
// counts 1's
int c2 = 0;
for (int i = 0; i < l; i++) {
if (s[i] == '0') {
c2 = 0;
// count consecutive 0's
c1++;
}
else {
c1 = 0;
// count consecutive 1's
c2++;
}
if (c1 == m || c2 == m)
return true;
}
return false;
}
// Drivers Code
int main()
{
string s = "001001";
int m = 2;
// function call
if (check(s, m))
cout << "YES";
else
cout << "NO";
return 0;
}
Java
// Program to check if the
// binary string contains
// m consecutive 1's or 0's
import java.io.*;
class GFG
{
// Function that checks if
// the binary string contains m
// consecutive 1's or 0's
static boolean check(String s,
int m)
{
// length of binary string
int l = s.length();
// counts zeros
int c1 = 0;
// counts 1's
int c2 = 0;
for (int i = 0; i < l; i++)
{
if (s.charAt(i) == '0')
{
c2 = 0;
// count consecutive 0's
c1++;
}
else
{
c1 = 0;
// count consecutive 1's
c2++;
}
if (c1 == m || c2 == m)
return true;
}
return false;
}
// Drivers Code
public static void main (String[] args)
{
String s = "001001";
int m = 2;
// function call
if (check(s, m))
System.out.println( "YES");
else
System.out.println( "NO");
}
}
// This code is contributed by anuj_67.
Python 3
# Program to check if the binary string
# contains m consecutive 1's or 0's
# Function that checks if
# the binary string contains m
# consecutive 1's or 0's
def check(s, m):
# length of binary string
l = len(s);
# counts zeros
c1 = 0;
# counts 1's
c2 = 0;
for i in range(0, l - 1):
if (s[i] == '0'):
c2 = 0;
# count consecutive 0's
c1 = c1 + 1;
else :
c1 = 0;
# count consecutive 1's
c2 = c2 + 1;
if (c1 == m or c2 == m):
return True;
return False;
# Driver Code
s = "001001";
m = 2;
# function call
if (check(s, m)):
print("YES");
else :
print("NO");
# This code is contributed
# by Shivi_Agggarwal
C#
// Program to check if the
// binary string contains
// m consecutive 1's or 0's
using System;
class GFG
{
// Function that checks if
// the binary string contains
// m consecutive 1's or 0's
static bool check(string s,
int m)
{
// length of
// binary string
int l = s.Length;
// counts zeros
int c1 = 0;
// counts 1's
int c2 = 0;
for (int i = 0; i < l; i++)
{
if (s[i] == '0')
{
c2 = 0;
// count consecutive
// 0's
c1++;
}
else
{
c1 = 0;
// count consecutive
// 1's
c2++;
}
if (c1 == m || c2 == m)
return true;
}
return false;
}
// Driver Code
public static void Main ()
{
String s = "001001";
int m = 2;
// function call
if (check(s, m))
Console.WriteLine( "YES");
else
Console.WriteLine( "NO");
}
}
// This code is contributed
// by anuj_67.
PHP
输出 :
YES
时间复杂度: O(N),其中N是二进制字符串的长度。
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。