text: Count number of strings ( made of R , G and B ) using given combination | Function to calculate number of strings ; Store factorial of numbers up to n for further computation ; Find the remaining values to be added ; Make all possible combinations of R , B and G for the remaining value ; Compute permutation of each combination one by one and add them . ; Return total no . of strings / permutation ; Driver Code
code: < ? php function possibleStrings ( $ n , $ r , $ b , $ g ) { $ fact [ 0 ] = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ fact [ $ i ] = $ fact [ $ i - 1 ] * $ i ; $ left = $ n - ( $ r + $ g + $ b ) ; $ sum = 0 ; for ( $ i = 0 ; $ i <= $ left ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ left - $ i ; $ j ++ ) { $ k = $ left - ( $ i + $ j ) ; $ sum = $ sum + $ fact [ $ n ] / ( $ fact [ $ i + $ r ] * $ fact [ $ j + $ b ] * $ fact [ $ k + $ g ] ) ; } } return $ sum ; } $ n = 4 ; $ r = 2 ; $ b = 0 ; $ g = 1 ; echo possibleStrings ( $ n , $ r , $ b , $ g ) ; ? >