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


Three integers are given to us ‘a’, ‘b’, and ‘c’ represents the frequency of the three different characters ‘A’, ‘B’, and ‘C’. We have to find the number of different strings that can be formed by using these characters and there must be at least two different characters present in the string formed. We will see two approaches for this problem one is the naive approach and another is the mathematical approach.

Sample Examples

Input 1: a = 3, b = 2, c = 4 
Output:  3 

Explanation

We can create three strings ‘ABC’, ‘ABC’, and ‘ACC’. We have used ‘A’ 3 times, ‘B’ 2 times, and ‘C’ 4 times in these strings which is the same or less than their given frequencies and it is all of the strings contains at least 2 different characters.

Input 2: a = 1, b = 3, c = 10
Output: 4

Explanation

We can create strings ‘ACC’, ‘BCC’, ‘BCC’, and ‘BCC’. We have used all the given characters except two ‘C’ as there were no other characters left to create the new string. If we have tried other combinations, then there will be lesser number of final strings.

Naive Approach

The naive approach is to find all the possible combinations with the given frequency but the thing is it will take a lot of time complexity and is highly inefficient to work with.

We have to generate all the possible substrings and if we have numbers are huge then it will take a lot of time and space which is not handled by the pc.

Mathematical Approach

Idea

The idea behind this approach is that we need at least two different characters in the string so we will always try to focus on the characters that have the least frequency.

The maximum number of strings that we can make are (a+b+c)/3 and the possible will only depend on the frequency of the least two.

Let us say, if the frequency of the least two is x and y and then the sum of both of them is greater than or equal to the (a+b+c)/3 then we can print this value as the answer otherwise sum of x and y will be the answer.

Implementation

We have seen the examples and the idea to find the solution, now le us move to implement the code −

  • First, we will create a function that will take three integers and return an integer.

  • In the function, first, we will store all the integers in a vector and later sort the vector to get the minimum frequency integers.

  • We will get the sum of all the given elements and divide by 3 to get the maximum number of strings we can make.

  • Later, we will compare the value of maximum strings with the lowest two-frequency sum. If the sum is less then we will update the maximum strings to the sum of least two frequency elements.

  • At last, we will return the value of the maximum possible strings and will print that in the main function.

Example

#include <bits/stdc++.h>
using namespace std;
int count(int a, int b, int c){
   // storing the values in the vector 
   vector<int>temp(3);
   temp[0] = a;
   temp[1] = b;
   temp[2] = c; 
   
   // sorting the vector to get the minimum two elements 
   sort(temp.begin(), temp.end());
   
   // counting the sum of all the elements 
   int maxStrings = (a+b+c)/3;    
   if(temp[0] + temp[1] < maxStrings){
      maxStrings = temp[0] + temp[1];
   }    
   return maxStrings; // returning the final answer
}
int main(){

   // given numbers 
   int a = 3;
   int b = 2;
   int c = 4;
   cout<<"The count of 3 length strings using given characters containing at least 2 different characters is "<<count(a,b,c)<<endl;   
   return 0;
}

Output

The count of 3 length strings using given characters containing at least 2 different characters is 3

Time and Space Complexity

The time complexity of the above code is O(1) or constant as we are not using any loop or recursive calls to get the result.

The space complexity of the above code is O(1), as we are not using extra space here.

Conclusion

In this tutorial, we have implemented a program to find the count of 3 length strings using given characters containing at least 2 different characters is 3. We have discussed the naive approach and implemented the mathematical approach with the constant time and space complexity that is O(1).

Updated on: 26-Jul-2023

52 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements