text: Check if a string has m consecutive 1 ' s ▁ or ▁ 0' s | 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 ; length of binary string ; counts zeros ; counts 1 's ; count consecutive 0 's ; count consecutive 1 's ; Drivers Code ; function call
code: #include <bits/stdc++.h> NEW_LINE #include <stdio.h> NEW_LINE 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 ; }