Count of 3 sized Strings of all same or different characters using total X 0s, Y 1s and Z 2s
Given three integers X, Y and Z denoting the frequencies of three different characters ‘0‘, ‘1‘, and ‘2’ respectively. the task is to find the maximum number of valid strings of length three that can be formed using the given frequencies such that in each string all the three characters are same or all are different.
Examples:
Input: X = 3, Y = 5, Z = 5
Output: 4
Explanation: Valid strings that can be obtained from the given frequencies: “102”, “012”, “111”, “222”.
Number of strings = 4.
Input: X = 8, Y = 8, Z = 9
Output: 8
Approach: The given problem can be solved by the following observations:
- If there are no such strings that contain all 3 different characters then the answer will always be (X/3 + Y/3 + Z/3).
- There always exists an optimal solution with less than 3 strings containing all the different characters. Because 3 such strings which contain all the 3 different characters can be changed to (1 all ‘0’ character string + 1 all ‘1’ character string + 1 all ‘2’ character string).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxValidStrings( int X, int Y, int Z)
{
int ans = 0;
for ( int i = 0; i < 3; i++) {
if (i > X || i > Y || i > Z) {
continue ;
}
int xRemain = X - i;
int yRemain = Y - i;
int zRemain = Z - i;
ans = max(ans, i + (xRemain / 3)
+ (yRemain / 3)
+ (zRemain / 3));
}
return ans;
}
int main()
{
int X = 8, Y = 8, Z = 9;
cout << maxValidStrings(X, Y, Z);
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static int maxValidStrings( int X, int Y, int Z)
{
int ans = 0 ;
for ( int i = 0 ; i < 3 ; i++) {
if (i > X || i > Y || i > Z) {
continue ;
}
int xRemain = X - i;
int yRemain = Y - i;
int zRemain = Z - i;
ans = Math.max(ans, i + (xRemain / 3 )
+ (yRemain / 3 )
+ (zRemain / 3 ));
}
return ans;
}
public static void main(String[] args)
{
int X = 8 , Y = 8 , Z = 9 ;
System.out.print(maxValidStrings(X, Y, Z));
}
}
|
Python3
def maxValidStrings( X, Y, Z):
ans = 0 ;
for i in range ( 3 ):
if (i > X or i > Y or i > Z):
continue ;
xRemain = X - i;
yRemain = Y - i;
zRemain = Z - i;
ans = max (ans, i + (xRemain / / 3 )
+ (yRemain / / 3 )
+ (zRemain / / 3 ));
return ans;
X = 8 ;
Y = 8 ;
Z = 9 ;
print (maxValidStrings(X, Y, Z));
|
C#
using System;
class GFG {
static int maxValidStrings( int X, int Y, int Z)
{
int ans = 0;
for ( int i = 0; i < 3; i++) {
if (i > X || i > Y || i > Z) {
continue ;
}
int xRemain = X - i;
int yRemain = Y - i;
int zRemain = Z - i;
ans = Math.Max(ans, i + (xRemain / 3)
+ (yRemain / 3)
+ (zRemain / 3));
}
return ans;
}
public static void Main()
{
int X = 8, Y = 8, Z = 9;
Console.Write(maxValidStrings(X, Y, Z));
}
}
|
Javascript
<script>
const maxValidStrings = (X, Y, Z) => {
let ans = 0;
for (let i = 0; i < 3; i++) {
if (i > X || i > Y || i > Z) {
continue ;
}
let xRemain = X - i;
let yRemain = Y - i;
let zRemain = Z - i;
ans = Math.max(ans, i + parseInt(xRemain / 3)
+ parseInt(yRemain / 3)
+ parseInt(zRemain / 3));
}
return ans;
}
let X = 8, Y = 8, Z = 9;
document.write(maxValidStrings(X, Y, Z));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
08 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...