Count of 3 sized Strings of all same or different characters using total X 0s, Y 1s and Z 2s


In this problem, we will count the number of strings we can create using the given frequencies such that the string contains same or different characters.

We have four options to create a string of length 3 using the 0, 1, and 2 characters. The first string is 012, 000, 111, and 222. So, we need to count the total number of such strings to get the answer.

Problem statement – We have given three integer values: X, Y, and Z. The X represents the frequency of the ‘0’, Y represents the frequency of the ‘1’, and Z represents the frequency of ‘Z’. The given task is to use the 0, 1, and 2 characters to create a strings of length 3, which contains all different or similar characters. So, we need to count the total number of such strings that we can create using the given frequencies.

Sample examples

Input

X = 3, Y = 4, Z = 4

Output

3

Explanation – The resultant strings are 012, 012, and 012.

Input

X = 1, Y = 4, Z = 4

Output

3

Explanation – The resultant strings are 012, 111, and 222.

Input

X = 1, Y = 2, Z = 10

Output

4

Explanation – The resultant strings are 012, 222, 222, and 222.

Approach 1

We can notice the four different strings which we can create using the given frequencies and conditions. So, if we say that the string containing different characters doesn’t exist, the answer is X/3 + Y/3 + Z/3.

If a string containing a different character exists, we can take a maximum of 2 strings of such type. If we take 3 strings containing the different characters, we can convert them into strings containing all 1s, 0s, and 2s. For example, we can convert 012, 201, and 102 to 000, 111, and 222.

So, we can get an answer by choosing at most two strings containing different characters and other strings containing the same characters.

Algorithm

Step 1 – Define the ‘cntStr’ variable and initialize it with zero to store string counts.

Step 2 – Use the loop to make 3 iterations from 0 to 2.

Step 3 – In the loop, If p is greater than X, Y, or Z, move to the next iteration, as we can’t create p strings containing different characters.

Step 4 – Subtract the p from all frequencies and store updated frequencies in new variables. We have subtracted p to create p different strings.

Step 5 – If the ‘cntStr’ is less than p + (freq0Left / 3) + (freq1Left / 3) + (freq2Left / 3), update its value. Here, we divide the updated frequency by 3 and take its sum to get the number of strings with the same characters.

Step 6 – Return the ‘cntStr’ value.

Example

#include <bits/stdc++.h>
using namespace std;

int totalStrings(int freq0, int freq1, int freq2) {
    // to store valid strings
    int cntStr = 0;
    for (int p = 0; p < 3; p++) {
        // Move to the next iteration if any frequency is smaller than p, as we can't make p strings with different characters
        if (p > freq0 || p > freq1 || p > freq2) {
            continue;
        }
        // Store the remaining characters left
        int freq0Left = freq0 - p;
        int freq1Left = freq1 - p;
        int freq2Left = freq2 - p;
        // Get the maximum number of strings
        cntStr = max(cntStr, p + (freq0Left / 3) + (freq1Left / 3) + (freq2Left / 3));
    }
    // Return the final result
    return cntStr;
}
int main(){
    int X = 3, Y = 4, Z = 4;
    cout << "The total number of maximum valid strings is " << totalStrings(X, Y, Z);
    return 0;
}

Output

The total number of maximum valid strings is 3

Time complexity – O(1) as we make only 3 loop iterations in each case.

Space complexity – O(1), as we don’t use any extra space.

Another approach to solving the problem is taking a minimum frequency value among 3 and creating the same number of strings containing different characters. We can subtract the minimum frequency from the other 2 frequencies and create a string with the same characters. It may not give the maximum strings that we can create. So, it’s better to use the approach given above.

Updated on: 24-Aug-2023

32 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements