We need to make a string of size n. Each character of the string is either ‘R’, ‘B’ or ‘G’. In the final string there needs to be at least r number of ‘R’, at least b number of ‘B’ and at least g number of ‘G’ (such that r + g + b <= n). We need to find number of such strings possible.

**Examples:**

Input : n = 4, r = 1, b = 1, g = 1. Output: 36 No. of 'R' >= 1, No. of ‘G’ >= 1, No. of ‘B’ >= 1 and (No. of ‘R’) + (No. of ‘B’) + (No. of ‘G’) = n then following cases are possible: 1. RBGR and its 12 permutation 2. RBGB and its 12 permutation 3. RBGG and its 12 permutation Hence answer is 36.

Asked in : Directi

- As R, B and G have to be included atleast for given no. of times. Remaining values = n -(r + b + g).
- Make all combinations for the remaining values.
- Consider each element one by one for the remaining values and sum up all the permuations.
- Return total no. of permutations of all the combinations.

## C++

`// C++ program to count number of possible strings ` `// with n characters. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate number of strings ` `int` `possibleStrings( ` `int` `n, ` `int` `r, ` `int` `b, ` `int` `g) ` `{ ` ` ` `// Store factorial of numbers up to n ` ` ` `// for further computation ` ` ` `int` `fact[n+1]; ` ` ` `fact[0] = 1; ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `fact[i] = fact[i-1] * i; ` ` ` ` ` `// Find the remaining values to be added ` ` ` `int` `left = n - (r+g+b); ` ` ` `int` `sum = 0; ` ` ` ` ` `// Make all possible combinations ` ` ` `// of R, B and G for the remaining value ` ` ` `for` `(` `int` `i = 0; i <= left; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = 0; j<= left-i; j++) ` ` ` `{ ` ` ` `int` `k = left - (i+j); ` ` ` ` ` `// Compute permutation of each combination ` ` ` `// one by one and add them. ` ` ` `sum = sum + fact[n] / ` ` ` `(fact[i+r]*fact[j+b]*fact[k+g]); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return total no. of strings/permutation ` ` ` `return` `sum; ` `} ` ` ` `// Drivers code ` `int` `main() ` `{ ` ` ` `int` `n = 4, r = 2; ` ` ` `int` `b = 0, g = 1; ` ` ` `cout << possibleStrings(n, r, b, g); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count number of possible ` `// strings with n characters. ` ` ` `class` `GFG{ ` ` ` ` ` `//Function to calculate number of strings ` ` ` `static` `int` `possibleStrings( ` `int` `n, ` `int` `r, ` `int` `b, ` `int` `g) ` ` ` `{ ` ` ` `// Store factorial of numbers up to n ` ` ` `// for further computation ` ` ` `int` `fact[] = ` `new` `int` `[n+` `1` `]; ` ` ` `fact[` `0` `] = ` `1` `; ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) ` ` ` `fact[i] = fact[i-` `1` `] * i; ` ` ` ` ` `// Find the remaining values to be added ` ` ` `int` `left = n - (r+g+b); ` ` ` `int` `sum = ` `0` `; ` ` ` ` ` `// Make all possible combinations ` ` ` `// of R, B and G for the remaining value ` ` ` `for` `(` `int` `i = ` `0` `; i <= left; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = ` `0` `; j<= left-i; j++) ` ` ` `{ ` ` ` `int` `k = left - (i+j); ` ` ` ` ` `// Compute permutation of each combination ` ` ` `// one by one and add them. ` ` ` `sum = sum + fact[n] / ` ` ` `(fact[i+r]*fact[j+b]*fact[k+g]); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return total no. of strings/permutation ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `//Drivers code ` ` ` `public` `static` `void` `main(String []args) ` ` ` `{ ` ` ` `int` `n = ` `4` `, r = ` `2` `; ` ` ` `int` `b = ` `0` `, g = ` `1` `; ` ` ` `System.out.println(possibleStrings(n, r, b, g)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to count number of ` `# possible strings with n characters. ` ` ` `# Function to calculate number of strings ` `def` `possibleStrings(n, r, b, g): ` ` ` ` ` `# Store factorial of numbers up to n ` ` ` `# for further computation ` ` ` `fact ` `=` `[` `0` `for` `i ` `in` `range` `(n ` `+` `1` `)] ` ` ` `fact[` `0` `] ` `=` `1` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `, ` `1` `): ` ` ` `fact[i] ` `=` `fact[i ` `-` `1` `] ` `*` `i ` ` ` ` ` `# Find the remaining values to be added ` ` ` `left ` `=` `n ` `-` `(r ` `+` `g ` `+` `b) ` ` ` `sum` `=` `0` ` ` ` ` `# Make all possible combinations of ` ` ` `# R, B and G for the remaining value ` ` ` `for` `i ` `in` `range` `(` `0` `, left ` `+` `1` `, ` `1` `): ` ` ` `for` `j ` `in` `range` `(` `0` `, left ` `-` `i ` `+` `1` `, ` `1` `): ` ` ` `k ` `=` `left ` `-` `(i ` `+` `j) ` ` ` ` ` `# Compute permutation of each ` ` ` `# combination one by one and add them. ` ` ` `sum` `=` `(` `sum` `+` `fact[n] ` `/` `(fact[i ` `+` `r] ` `*` ` ` `fact[j ` `+` `b] ` `*` `fact[k ` `+` `g])) ` ` ` ` ` `# Return total no. of ` ` ` `# strings/permutation ` ` ` `return` `sum` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `4` ` ` `r ` `=` `2` ` ` `b ` `=` `0` ` ` `g ` `=` `1` ` ` `print` `(` `int` `(possibleStrings(n, r, b, g))) ` ` ` `# This code is contributed by ` `# Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count number of possible ` `// strings with n characters. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `//Function to calculate number of strings ` ` ` `static` `int` `possibleStrings( ` `int` `n, ` `int` `r, ` ` ` `int` `b, ` `int` `g) ` ` ` `{ ` ` ` `// Store factorial of numbers up to n ` ` ` `// for further computation ` ` ` `int` `[] fact = ` `new` `int` `[n + 1]; ` ` ` `fact[0] = 1; ` ` ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `fact[i] = fact[i - 1] * i; ` ` ` ` ` `// Find the remaining values to be added ` ` ` `int` `left = n - (r + g + b); ` ` ` `int` `sum = 0; ` ` ` ` ` `// Make all possible combinations ` ` ` `// of R, B and G for the remaining value ` ` ` `for` `(` `int` `i = 0; i <= left; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = 0; j <= left - i; j++) ` ` ` `{ ` ` ` `int` `k = left - (i + j); ` ` ` ` ` `// Compute permutation of each combination ` ` ` `// one by one and add them. ` ` ` `sum = sum + fact[n] / (fact[i + r] * ` ` ` `fact[j + b] * fact[k + g]); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return total no. of strings/permutation ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `//Drivers code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 4, r = 2; ` ` ` `int` `b = 0, g = 1; ` ` ` `Console.WriteLine(possibleStrings(n, r, b, g)); ` ` ` `} ` `} ` ` ` `// This Code is contributed by Code_Mech. ` |

*chevron_right*

*filter_none*

## PHP

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

*chevron_right*

*filter_none*

**Output:**

22

To handle n with large numbers, we can use the concept of Large Factorial.

This article is contributed by **Sahil Chhabra**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Shortest possible combination of two strings
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Print all possible strings that can be made by placing spaces
- Print all possible strings that can be made by placing spaces
- Find the longest string that can be made up of other strings from the array
- Check if two strings can be made equal by swapping one character among each other
- Check whether two strings can be made equal by increasing prefixes
- Check whether two strings can be made equal by copying their characters with the adjacent ones
- Count the number of Special Strings of a given length N
- Count the number of common divisors of the given strings
- Count number of binary strings of length N having only 0's and 1's
- Count the Number of matching characters in a pair of strings
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count the number of strings in an array whose distinct characters are less than equal to M
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Count number of rotated strings which have more number of vowels in the first half than second half
- Count of same length Strings that exists lexicographically in between two given Strings
- Count of strings that become equal to one of the two strings after one removal
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Count of binary strings of length N having equal count of 0's and 1's