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

• Difficulty Level : Medium
• Last Updated : 12 Nov, 2021

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:

Input: a = 2, b = 3, c = 3
Output: 2
Explanation: Possible strings which satisfies the given conditions are: {“ABC”, “ABC”}

Input: a = 5, b = 4, c = 3
Output: 4

Approach: 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:

1. 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.
2. 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.
3. 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.
4. 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++

 // C++ implementation for the above approach#include using namespace std; // Function to count possible number of strings// such that each string consists of atleast// 2 different characters of length 3int countStrings(int a, int b, int c){    // Array to store the 3 frequencies    int arr;     arr = a;    arr = b;    arr = c;     // Total number of strings that can be    // formed irrespective of the given    // condition i.e, neglecting the condition    // that each string consists of atleast 2    // different characters of length 3    int count = (arr + arr + arr) / 3;     // Sort the array    sort(arr, arr + 3);     // If the sum of smallest and 2nd largest    // element is less than the count, then    // assign the sum to count    if (arr + arr < count) {        count = arr + arr;    }     // Return the count    return count;} // Driver Codeint main(){    int a = 5, b = 4, c = 3;     cout << countStrings(a, b, c);     return 0;}

Java

 // Java implementation for the above approachimport java.util.*; class GFG{    // Function to count possible number of strings    // such that each string consists of atleast    // 2 different characters of length 3    public static int countStrings(int a, int b, int c)    {               // Array to store the 3 frequencies        int[] arr = new int;         arr = a;        arr = b;        arr = c;         // Total number of strings that can be        // formed irrespective of the given        // condition i.e, neglecting the condition        // that each string consists of atleast 2        // different characters of length 3        int count = (arr + arr + arr) / 3;         // Sort the array        Arrays.sort(arr);         // If the sum of smallest and 2nd largest        // element is less than the count, then        // assign the sum to count        if (arr + arr < count) {            count = arr + arr;        }         // Return the count        return count;    }     // Driver Code    public static void main(String[] args) {                 int a = 5, b = 4, c = 3;         System.out.println(countStrings(a, b, c));    }} // This code is contributed by Samim Hossain Mondal

Python3

 # Python3 implementation for the above approach # Function to count possible number of strings# such that each string consists of atleast# 2 different characters of length 3def countStrings(a, b, c) :     # Array to store the 3 frequencies    arr = *3;     arr = a;    arr = b;    arr = c;     # Total number of strings that can be    # formed irrespective of the given    # condition i.e, neglecting the condition    # that each string consists of atleast 2    # different characters of length 3    count = (arr + arr + arr) // 3;     # Sort the array    arr.sort();     # If the sum of smallest and 2nd largest    # element is less than the count, then    # assign the sum to count    if (arr + arr < count) :        count = arr + arr;     # Return the count    return count; # Driver Codeif __name__ == "__main__" :     a = 5; b = 4; c = 3;     print(countStrings(a, b, c));     # This code is contributed by AnkThon

C#

 // C# code for the above approachusing System; public class GFG{       // Function to count possible number of strings    // such that each string consists of atleast    // 2 different characters of length 3    public static int countStrings(int a, int b, int c)    {         // Array to store the 3 frequencies        int[] arr = new int;         arr = a;        arr = b;        arr = c;         // Total number of strings that can be        // formed irrespective of the given        // condition i.e, neglecting the condition        // that each string consists of atleast 2        // different characters of length 3        int count = (arr + arr + arr) / 3;         // Sort the array        Array.Sort(arr);         // If the sum of smallest and 2nd largest        // element is less than the count, then        // assign the sum to count        if (arr + arr < count) {            count = arr + arr;        }         // Return the count        return count;    }     // Driver Code    static public void Main()    {         // Code        int a = 5, b = 4, c = 3;         Console.Write(countStrings(a, b, c));    }} // This code is contributed by Potta Lokesh

Javascript


Output
4

Time Complexity: O(1)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up