Count of Binary strings of length N having atmost M consecutive 1 s or 0 s alternatively exactly K times | C # program to find the count of binary strings of length N having atmost M consecutive 1 s or 0 s alternatively exactly K times ; Array to contain the readonly result ; Function to get the number of desirable binary strings ; If we reach end of string and groups are exhausted , return 1 ; If length is exhausted but groups are still to be made , return 0 ; If length is not exhausted but groups are exhausted , return 0 ; If both are negative just return 0 ; If already calculated , return it ; Initialise answer for each state ; Loop through every possible m ; Driver code
using System ; class GFG { static int [ , ] dp = new int [ 1000 , 1000 ] ; static int solve ( int n , int k , int m ) { if ( n == 0 && k == 0 ) return 1 ; if ( n == 0 && k != 0 ) return 0 ; if ( n != 0 && k == 0 ) return 0 ; if ( n < 0 k < 0 ) return 0 ; if ( dp [ n , k ] > 0 ) return dp [ n , k ] ; int ans = 0 ; for ( int j = 1 ; j <= m ; j ++ ) { ans += solve ( n - j , k - 1 , m ) ; } return dp [ n , k ] = ans ; } public static void Main ( String [ ] args ) { int N = 7 , K = 4 , M = 3 ; Console . Write ( solve ( N , K , M ) ) ; } }