Colorful Strings

Moderate
0/80
Average time to solve is 10m
profile
Contributed by
4 upvotes
Asked in companies
DirectiShareChat

Problem statement

You have been given an integer β€˜N’. You are supposed to find the number of all possible strings of length β€˜N’ where each character of the string is either β€˜R’, β€˜B’ or β€˜G’. In the final string, there must be at least β€˜r’ characters of β€˜R, at least β€˜b’ characters of β€˜B’ and at least β€˜g’ characters of β€˜G’.

Detailed explanation ( Input/output format, Notes, Images )

Input Format :

The first line contains an integer β€˜T’ denoting the number of test cases. Then each test case follows.

The first input line of each test case contains 4 space-separated integers β€˜N’, β€˜r’, β€˜b’ and β€˜g’ described as above in the problem.

Output Format :

For each test case, print the number of all possible strings of length β€˜N’ with constraints as described in the problem statement.

The output of each test case will be printed in a separate line.

Note:

You do not need to print anything; it has already been taken care of. Just implement the function.

Constraints :

1 <= T <= 50
1 <= N <= 20
1 <= r <= N
1 <= b <= N
1 <= g <= N
r + g + b <= N

Time limit: 1 sec

Sample Input 1 :

2
3 1 1 1
4 1 1 1

Sample Output 1 :

6
36

Explanation Of Sample Output 1 :

For the first test case, β€œRGB”, β€œRBG”, β€œGBR”, β€œGRB”, β€œBRG” and β€œBGR” are 6 possible permutations possible.

For the second test case, the following 3 cases are possible.
β€œRGBB” and its 12 permutations.
β€œRGBR” and its 12 permuations.
β€œRGBB” and its 12 permuations.
 So total permutations = 12 + 12 + 12 = 36

Sample Input 2 :

2
3 0 0 0 
2 1 1 0

Sample Output 2 :

27
2
Hint

Can you think of fixing the number of β€˜R’s, β€˜G’s and β€˜B’s used in the string?

Approaches (1)
Brute Force

The key idea of this approach is to fix the number of β€˜R’s , β€˜G’s and β€˜B’s in the string and count all the permutations.

 

Consider the following steps:

  1. Create an array/list β€œfact” where the i-th index will store i ! (i factorial). Update β€œfact” as fact[0] = 1.
  2. Create a loop using a variable β€˜i’ such that 1 <= i <= β€˜N’.
    1. Update β€œfact” as fact[i] = (fact[i - 1] * i ).
  3. Create and initialize a variable β€œans” to zero which stores the count of all possible strings.
  4. Create a loop using the variable β€˜i’ which denotes the count of β€˜R’s in the string such that r <= β€˜i’ <= β€˜N’.
    • Create a nested loop using a variable β€˜j’ which denotes the count of G’s in the string such that g <= β€˜j’ <= β€˜N’.
      • Store the count of β€˜B’ in a variable called β€˜k’ i.e. β€˜k’ = β€˜N’ - i - j.
      • If β€˜k’ is greater than or equal to β€˜b’ then we will add the number of permutations possible i.e. ans = ans + (fact[n] / (fact[i] * fact[ j] * fact[k]).
  5. Return β€œans”.
Time Complexity

O(N ^ 2), where β€˜N’ is the length of the string.

 

We are pre-computing the β€œfact” array/list which takes O(N) and then we are using a nested loop where each loop runs for O(N) time.  So the overall time complexity will be O(N) + O(N ^ 2) = O(N^2).

Space Complexity

O(N), where β€˜N’ is the length of the string.

 

Since we are using an array/list to store the factorial of each number upto β€˜N’. So the overall space complexity will be O(N).

Code Solution
(100% EXP penalty)
Colorful Strings
Full screen
Console