- Combinatorial, Mathematical, Strings

Count of 3 length strings using given characters containing at least 2 different characters

Given three integers a, b and c which denotes the frequencies of three different characters ‘A‘, ‘B‘, and ‘C‘ respectively, and can be used to form strings of length 3. The task is to count the total number of possible combinations of A, B and C such that it forms a string having at least 2 different characters.Example: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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.Input: a = 2, b = 3, c = 3Output: 2Explanation: Possible strings which satisfies the given conditions are: {“ABC”, “ABC”}Input: a = 5, b = 4, c = 3Output: 4Approach: Total number of strings of length 3, that can be formed with the given frequencies is (a+b+c)/3, assuming to select any character for any string. But as only strings with 2 different characters are required, so it’s essential to check that if that’s possible or not. To check that:Assume that, all (a+b+c)/3 strings are formed up to two places only with any and all does have a remaining space left to be filled.Now, till this point are strings are valid because:If the string has two different characters, then it’s valid.If the string has two same characters, then it can be made valid by inserting a different character.So, the total number of different characters we need is, let’s say count where count = (a+b+c)/3, assuming that one character is required for each string.So if the sum of the two smallest frequencies exceeds count, then (a+b+c)/3 number of strings can be formed. Otherwise, it would be the sum of two smallest frequencies.Below is the implementation of the above approach:C++#include using namespace std;  int countStrings(int a, int b, int c){        int arr[3];      arr[0] = a;    arr[1] = b;    arr[2] = c;                          int count = (arr[0] + arr[1] + arr[2]) / 3;          sort(arr, arr + 3);                  if (arr[0] + arr[1] < count) {        count = arr[0] + arr[1];    }          return count;}  int main(){    int a = 5, b = 4, c = 3;      cout