Check if a string has m consecutive 1’s or 0’s

27 May, 2021
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++
Java
Python 3
C#
PHP
Javascript
// Program to check if the binary string// contains m consecutive 1's or 0's#include <bits/stdc++.h>#include <stdio.h>using namespace std; // Function that checks if// the binary string contains m// consecutive 1's or 0'sbool 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 Codeint main(){    string s = "001001";    int m = 2;     // function call    if (check(s, m))        cout << "YES";    else        cout << "NO";     return 0;}
// Program to check if the// binary string contains// m consecutive 1's or 0'simport java.io.*; class GFG{ // Function that checks if// the binary string contains m// consecutive 1's or 0'sstatic 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.
# 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'sdef 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 Codes = "001001";m = 2; # function callif (check(s, m)):    print("YES");else :    print("NO"); # This code is contributed# by Shivi_Agggarwal
// Program to check if the// binary string contains// m consecutive 1's or 0'susing System; class GFG{ // Function that checks if// the binary string contains// m consecutive 1's or 0'sstatic 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 Codepublic 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// 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'sfunction check($s, $m){    // length of binary    // string    $l = count($s);     // counts zeros    $c1 = 0;     // counts 1's    $c2 = 0;     for ($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 or            $c2 == $m)            return true;    }    return false;} // Driver Code$s = "001001";$m = 2; // function callif (check($s, $m))    echo "YES";else    echo "NO"; // This code is contributed// by anuj_67.?>
<script>    // 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    function check(s, m)    {        // length of        // binary string        let l = s.length;         // counts zeros        let c1 = 0;         // counts 1's        let c2 = 0;         for (let 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;    }         let s = "001001";    let m = 2;           // function call    if (check(s, m))        document.write( "YES");    else        document.write( "NO");     </script>
YES
 
Time Complexity: O(N), where N is the length of the binary string.  
vt_m
Shivi_Aggarwal
suresh07
binary-string
C++ Programs
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Passing a function as a parameter in C++
Const keyword in C++
cout in C++
Program to implement Singly Linked List in C++ using class
Different ways to print elements of vector
Write a program to reverse an array or string
Reverse a string in Java
Write a program to print all permutations of a given string
C++ Data Types
Check for Balanced Brackets in an expression (well-formedness) using Stack