📌  相关文章
📜  使用包含至少 2 个不同字符的给定字符的 3 个长度字符串的计数

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

使用包含至少 2 个不同字符的给定字符的 3 个长度字符串的计数

给定三个整数abc ,分别表示三个不同字符' A '、' B '和' C '出现的频率,可以组成长度为3的字符串。任务是统计所有字符的总数A、B 和 C 的可能组合,使其形成一个具有至少2 个不同字符的字符串。

例子:

方法:长度为 3 的字符串总数,可以用给定的频率形成为(a+b+c)/3,假设为任何字符串选择任何字符。但由于只需要具有 2 个不同字符的字符串,因此必须检查是否可能。要检查:

  1. 假设,所有(a+b+c)/3字符串仅由任意两个位置组成,并且所有字符串都有剩余空间待填充。
  2. 现在,到目前为止,字符串都是有效的,因为:
    • 如果字符串有两个不同的字符,那么它是有效的。
    • 如果字符串有两个相同的字符,则可以通过插入不同的字符使其有效。
  3. 因此,我们需要的不同字符的总数是count where count = (a+b+c)/3 ,假设每个字符串需要一个字符。
  4. 因此,如果两个最小频率之和超过count ,则可以形成(a+b+c)/3个字符串。否则,它将是两个最小频率的总和。

下面是上述方法的实现:

C++
// C++ implementation for the above approach
#include 
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


输出
4

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