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 traversing the binary string, keep a count of the number of 1’s or 0’s appearing consecutively. If there are M consecutive 1’s or 0’s, return True, else return False.
Given below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
bool check(string s, int m)
{
int l = s.length();
int c1 = 0;
int c2 = 0;
for ( int i = 0; i < l; i++) {
if (s[i] == '0' ) {
c2 = 0;
c1++;
}
else {
c1 = 0;
c2++;
}
if (c1 == m || c2 == m)
return true ;
}
return false ;
}
int main()
{
string s = "001001" ;
int m = 2;
if (check(s, m))
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static boolean check(String s,
int m)
{
int l = s.length();
int c1 = 0 ;
int c2 = 0 ;
for ( int i = 0 ; i < l; i++)
{
if (s.charAt(i) == '0' )
{
c2 = 0 ;
c1++;
}
else
{
c1 = 0 ;
c2++;
}
if (c1 == m || c2 == m)
return true ;
}
return false ;
}
public static void main (String[] args)
{
String s = "001001" ;
int m = 2 ;
if (check(s, m))
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python 3
def check(s, m):
l = len (s);
c1 = 0 ;
c2 = 0 ;
for i in range ( 0 , l - 1 ):
if (s[i] = = '0' ):
c2 = 0 ;
c1 = c1 + 1 ;
else :
c1 = 0 ;
c2 = c2 + 1 ;
if (c1 = = m or c2 = = m):
return True ;
return False ;
s = "001001" ;
m = 2 ;
if (check(s, m)):
print ( "YES" );
else :
print ( "NO" );
|
C#
using System;
class GFG
{
static bool check( string s,
int m)
{
int l = s.Length;
int c1 = 0;
int c2 = 0;
for ( int i = 0; i < l; i++)
{
if (s[i] == '0' )
{
c2 = 0;
c1++;
}
else
{
c1 = 0;
c2++;
}
if (c1 == m || c2 == m)
return true ;
}
return false ;
}
public static void Main ()
{
String s = "001001" ;
int m = 2;
if (check(s, m))
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
PHP
<?php
function check( $s , $m )
{
$l = count ( $s );
$c1 = 0;
$c2 = 0;
for ( $i = 0; $i <= $l ; $i ++)
{
if ( $s [ $i ] == '0' )
{
$c2 = 0;
$c1 ++;
}
else
{
$c1 = 0;
$c2 ++;
}
if ( $c1 == $m or
$c2 == $m )
return true;
}
return false;
}
$s = "001001" ;
$m = 2;
if (check( $s , $m ))
echo "YES" ;
else
echo "NO" ;
?>
|
Javascript
<script>
function check(s, m)
{
let l = s.length;
let c1 = 0;
let c2 = 0;
for (let i = 0; i < l; i++)
{
if (s[i] == '0 ')
{
c2 = 0;
// count consecutive
// 0' s
c1++;
}
else
{
c1 = 0;
c2++;
}
if (c1 == m || c2 == m)
return true ;
}
return false ;
}
let s = "001001" ;
let m = 2;
if (check(s, m))
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Time Complexity: O(N), where N is the length of the binary string.
Auxiliary Space: O(1), as no extra space has been used
Last Updated :
17 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...