Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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:

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 = 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 <bits/stdc++.h>
using namespace std;
 
// Function to count possible number of strings
// such that each string consists of atleast
// 2 different characters of length 3
int countStrings(int a, int b, int c)
{
    // Array to store the 3 frequencies
    int arr[3];
 
    arr[0] = a;
    arr[1] = b;
    arr[2] = 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[0] + arr[1] + arr[2]) / 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[0] + arr[1] < count) {
        count = arr[0] + arr[1];
    }
 
    // Return the count
    return count;
}
 
// Driver Code
int main()
{
    int a = 5, b = 4, c = 3;
 
    cout << countStrings(a, b, c);
 
    return 0;
}

Java




// Java implementation for the above approach
import 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[3];
 
        arr[0] = a;
        arr[1] = b;
        arr[2] = 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[0] + arr[1] + arr[2]) / 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[0] + arr[1] < count) {
            count = arr[0] + arr[1];
        }
 
        // 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 3
def countStrings(a, b, c) :
 
    # Array to store the 3 frequencies
    arr = [0]*3;
 
    arr[0] = a;
    arr[1] = b;
    arr[2] = 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[0] + arr[1] + arr[2]) // 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[0] + arr[1] < count) :
        count = arr[0] + arr[1];
 
    # Return the count
    return count;
 
# Driver Code
if __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 approach
using 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[3];
 
        arr[0] = a;
        arr[1] = b;
        arr[2] = 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[0] + arr[1] + arr[2]) / 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[0] + arr[1] < count) {
            count = arr[0] + arr[1];
        }
 
        // 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




<script>
 
// Javascript implementation for the above approach
 
// Function to count possible number of strings
// such that each string consists of atleast
// 2 different characters of length 3
function countStrings(a, b, c)
{
    // Array to store the 3 frequencies
    var arr = [0,0,0];
 
    arr[0] = a;
    arr[1] = b;
    arr[2] = 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
    var count = parseInt((arr[0] + arr[1] + arr[2]) / 3);
 
    // Sort the array
    arr.sort((a,b) => a-b);
 
    // If the sum of smallest and 2nd largest
    // element is less than the count, then
    // assign the sum to count
    if (arr[0] + arr[1] < count) {
        count = arr[0] + arr[1];
    }
 
    // Return the count
    return count;
}
 
// Driver Code
var a = 5, b = 4, c = 3;
document.write(countStrings(a, b, c));
 
// This code is contributed by rutvik_56.
</script>
Output
4

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :