Skip to content

Commit b586086

Browse files
committed
Colorful Strings
1 parent 89af43e commit b586086

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// { Driver Code Starts
2+
// Initial Template for C++
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
// } Driver Code Ends
7+
// User function Template for C++
8+
class Solution {
9+
public:
10+
long long possibleStrings(int n, int r, int b, int g)
11+
{
12+
// code here
13+
long long fact[21];
14+
fact[0] = 1;
15+
for(int i = 1 ; i <= 20 ; i++)
16+
fact[i] = fact[i-1] * i;
17+
long long res = 0, sum = n - (r + b + g);
18+
for(int i = 0 ; i <= sum ; i++)
19+
{
20+
for(int j = 0 ; j <= sum ; j++)
21+
{
22+
if(sum - (i + j) >= 0)
23+
res += fact[n] / (fact[r + i] * fact[g + j] * fact[sum - i - j + b]);
24+
}
25+
}
26+
return res;
27+
}
28+
};
29+
30+
// { Driver Code Starts.
31+
int main() {
32+
int t;
33+
cin >> t;
34+
while (t--) {
35+
int n, r, g, b;
36+
cin >> n >> r >> g >> b;
37+
Solution ob;
38+
cout << ob.possibleStrings(n, r, b, g) << endl;
39+
}
40+
return 0;
41+
}
42+
// } Driver Code Ends

Some_More_Strings/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,15 @@
9797
Input: s1 = aacdb, s2 = gafd
9898
Output: cbgf
9999
Explanation: The common characters of s1 and s2 are: a, d. The uncommon characters of s1 and s2 are c, b, g and f. Thus the modified string with uncommon characters concatenated is cbgf.
100+
101+
## 12. Colorful Strings:
102+
Find the count of all possible strings 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).
103+
Example 1:
104+
Input: n = 4, r = 1, g = 1, b = 1
105+
Output: 36
106+
Explanation: No. of 'R' >= 1,
107+
No. of ‘G’ >= 1, No. of ‘B’ >= 1 and (No. of ‘R’) + (No. of ‘B’) + (No. of ‘G’) = n then following cases are possible:
108+
1. RBGR and its 12 permutation
109+
2. RBGB and its 12 permutation
110+
3. RBGG and its 12 permutation
111+
Hence answer is 36.

0 commit comments

Comments
 (0)