Find the number of binary strings of length N with at least 3 consecutive 1 s | Function to return the count of required strings ; '0' at ith position ; '1' at ith position ; Driver code ; Base condition : 2 ^ ( i + 1 ) because of 0 indexing
def solve ( i , x , dp ) : NEW_LINE INDENT if ( i < 0 ) : NEW_LINE INDENT return x == 3 NEW_LINE DEDENT if ( dp [ i ] [ x ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ x ] NEW_LINE DEDENT dp [ i ] [ x ] = solve ( i - 1 , 0 , dp ) NEW_LINE dp [ i ] [ x ] += solve ( i - 1 , x + 1 , dp ) NEW_LINE return dp [ i ] [ x ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 ; NEW_LINE dp = [ [ 0 for i in range ( n ) ] for j in range ( 4 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( 4 ) : NEW_LINE INDENT dp [ i ] [ j ] = - 1 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT dp [ i ] [ 3 ] = ( 1 << ( i + 1 ) ) NEW_LINE DEDENT print ( solve ( n - 1 , 0 , dp ) ) NEW_LINE DEDENT