Count of strings that can be formed using a , b and c under given constraints | n is total number of characters . bCount and cCount are counts of ' b ' and ' c ' respectively . ; Base cases ; if we had saw this combination previously ; Three cases , we choose , a or b or c In all three cases n decreases by 1. ; A wrapper over countStrUtil ( ) ; Driver code ; Total number of characters
def countStrUtil ( dp , n , bCount = 1 , cCount = 2 ) : NEW_LINE INDENT if ( bCount < 0 or cCount < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( bCount == 0 and cCount == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( dp [ n ] [ bCount ] [ cCount ] != - 1 ) : NEW_LINE INDENT return dp [ n ] [ bCount ] [ cCount ] NEW_LINE DEDENT res = countStrUtil ( dp , n - 1 , bCount , cCount ) NEW_LINE res += countStrUtil ( dp , n - 1 , bCount - 1 , cCount ) NEW_LINE res += countStrUtil ( dp , n - 1 , bCount , cCount - 1 ) NEW_LINE dp [ n ] [ bCount ] [ cCount ] = res NEW_LINE return dp [ n ] [ bCount ] [ cCount ] NEW_LINE DEDENT def countStr ( n ) : NEW_LINE INDENT dp = [ [ [ - 1 for x in range ( n + 2 ) ] for y in range ( 3 ) ] for z in range ( 4 ) ] NEW_LINE return countStrUtil ( dp , n ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE print ( countStr ( n ) ) NEW_LINE DEDENT