Find the number of binary strings of length N with at least 3 consecutive 1 s | C # implementation of the approach value x ; Function that returns true if s contains three consecutive 1 's ; Function to return the count of required strings ; Driver code
using System ; class GFG { static bool check ( String s ) { int n = s . Length ; for ( int i = 2 ; i < n ; i ++ ) { if ( s [ i ] == '1' && s [ i - 1 ] == '1' && s [ i - 2 ] == '1' ) return true ; } return false ; } static int countStr ( int i , String s ) { if ( i < 0 ) { if ( check ( s ) ) return 1 ; return 0 ; } char [ ] myNameChars = s . ToCharArray ( ) ; myNameChars [ i ] = '0' ; s = String . Join ( " " , myNameChars ) ; int ans = countStr ( i - 1 , s ) ; char [ ] myChar = s . ToCharArray ( ) ; myChar [ i ] = '1' ; s = String . Join ( " " , myChar ) ; ans += countStr ( i - 1 , s ) ; char [ ] myChar1 = s . ToCharArray ( ) ; myChar1 [ i ] = '0' ; s = String . Join ( " " , myChar1 ) ; return ans ; } public static void Main ( String [ ] args ) { int N = 4 ; String s = "0000" ; Console . WriteLine ( countStr ( N - 1 , s ) ) ; } }