📌  相关文章
📜  使用总共 X 0、Y 1 和 Z 2 计算所有相同或不同字符的 3 个大小的字符串

📅  最后修改于: 2022-05-13 01:56:08.363000             🧑  作者: Mango

使用总共 X 0、Y 1 和 Z 2 计算所有相同或不同字符的 3 个大小的字符串

给定三个整数XYZ ,分别表示三个不同字符“ 0 ”、“ 1 ”和“2”的频率。任务是找到可以使用给定频率形成的长度为 3 的有效字符串的最大数量,使得在每个字符串中所有三个字符都相同或都不同。

例子:

方法:给定的问题可以通过以下观察来解决:

  • 如果没有包含所有 3 个不同字符的此类字符串,则答案将始终为 ( X/3 + Y/3 + Z/3 )。
  • 总是存在一个包含所有不同字符的字符串少于 3 个的最优解。因为包含所有 3 个不同字符的 3 个这样的字符串可以更改为(1 个全为 '0'字符+ 1 个全为 '1'字符串+ 1字符串全字符串) 。

下面是上述方法的实现:

C++
// C++ code to implement the approach
 
#include 
using namespace std;
 
// Function to find the maximum valid
// strings that can be formed from
// the given frequencies
int maxValidStrings(int X, int Y, int Z)
{
    // Variable to store the answer
    int ans = 0;
    for (int i = 0; i < 3; i++) {
 
        // If i is greater than any of
        // the frequencies then continue
        if (i > X || i > Y || i > Z) {
            continue;
        }
 
        // Store the remaining characters left
        int xRemain = X - i;
        int yRemain = Y - i;
        int zRemain = Z - i;
 
        // Store the maximum one
        ans = max(ans, i + (xRemain / 3)
                           + (yRemain / 3)
                           + (zRemain / 3));
    }
 
    // Return ans
    return ans;
}
 
// Driver Code
int main()
{
    int X = 8, Y = 8, Z = 9;
 
    // Function call
    cout << maxValidStrings(X, Y, Z);
    return 0;
}


Java
// JAVA code to implement the approach
import java.util.*;
class GFG
{
 
  // Function to find the maximum valid
  // strings that can be formed from
  // the given frequencies
  public static int maxValidStrings(int X, int Y, int Z)
  {
 
    // Variable to store the answer
    int ans = 0;
    for (int i = 0; i < 3; i++) {
 
      // If i is greater than any of
      // the frequencies then continue
      if (i > X || i > Y || i > Z) {
        continue;
      }
 
      // Store the remaining characters left
      int xRemain = X - i;
      int yRemain = Y - i;
      int zRemain = Z - i;
 
      // Store the maximum one
      ans = Math.max(ans, i + (xRemain / 3)
                     + (yRemain / 3)
                     + (zRemain / 3));
    }
 
    // Return ans
    return ans;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int X = 8, Y = 8, Z = 9;
 
    // Function call
    System.out.print(maxValidStrings(X, Y, Z));
  }
}
 
// This code is contributed by Taranpreet


Python3
# Python code for the above approach
 
# Function to find the maximum valid
# strings that can be formed from
# the given frequencies
def maxValidStrings( X,  Y,  Z):
 
    # Variable to store the answer
    ans = 0;
    for i in range(3):
 
        # If i is greater than any of
        # the frequencies then continue
        if (i > X or i > Y or i > Z):
            continue;
         
        # Store the remaining characters left
        xRemain = X - i;
        yRemain = Y - i;
        zRemain = Z - i;
 
        # Store the maximum one
        ans = max(ans, i + (xRemain // 3)
                           + (yRemain // 3)
                           + (zRemain // 3));
     
 
    # Return ans
    return ans;
 
# Driver Code
X = 8;
Y = 8;
Z = 9;
 
    # Function call
print(maxValidStrings(X, Y, Z));
    
# This code is contributed by Potta Lokesh


C#
// C# code to implement the approach
using System;
class GFG {
 
  // Function to find the maximum valid
  // strings that can be formed from
  // the given frequencies
  static int maxValidStrings(int X, int Y, int Z)
  {
 
    // Variable to store the answer
    int ans = 0;
    for (int i = 0; i < 3; i++) {
 
      // If i is greater than any of
      // the frequencies then continue
      if (i > X || i > Y || i > Z) {
        continue;
      }
 
      // Store the remaining characters left
      int xRemain = X - i;
      int yRemain = Y - i;
      int zRemain = Z - i;
 
      // Store the maximum one
      ans = Math.Max(ans, i + (xRemain / 3)
                     + (yRemain / 3)
                     + (zRemain / 3));
    }
 
    // Return ans
    return ans;
  }
 
  // Driver Code
  public static void Main()
  {
    int X = 8, Y = 8, Z = 9;
 
    // Function call
    Console.Write(maxValidStrings(X, Y, Z));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
8

时间复杂度: O(1)
辅助空间: O(1)