📌  相关文章
📜  每个字符出现频率最多为 K 的字符串数

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

每个字符出现频率最多为 K 的字符串数

给定一个包含N个字符串和一个整数K的数组arr[] ,任务是找到每个字符的频率最多为K的字符串计数

例子:

方法:想法是遍历字符串数组,并为每个字符串创建一个字符频率图。只要任何字符的频率大于K ,就跳过当前字符串。否则,如果没有字符的频率超过K ,则增加answer的计数。当遍历所有字符串时,打印存储在答案中的计数。请按照以下步骤解决问题:

  • 定义一个函数isDuogram(字符串 s, int K)并执行以下任务:
    • 用值0初始化向量freq[26]
    • 使用变量c遍历字符串s并执行以下任务:
      • 频率计数增加1。
    • 迭代范围[0. 26)使用变量i并执行以下任务:
      • 如果freq[i]大于K则返回false。
    • 执行上述步骤后,返回true作为答案。
  • 将变量ans初始化为0以存储答案。
  • 使用变量x遍历数组arr[]并执行以下任务:
    • 调用函数isDuogram(x, K) ,如果函数返回true ,则将ans的计数增加1。
  • 执行上述步骤后,打印ans的值作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if a string
// is an duogram or not
bool isDuogram(string s, int K)
{
 
    // Array to store the frequency
    // of characters
    vector freq(26, 0);
 
    // Count the frequency
    for (char c : s) {
        freq++;
    }
 
    // Check if the frequency is greater
    // than K or not
    for (int i = 0; i < 26; i++)
        if (freq[i] > K)
            return false;
 
    return true;
}
 
// Function to check if array arr contains
// all duograms or not
int countDuograms(vector& arr, int K)
{
 
    // Store the answer
    int ans = 0;
 
    // Traverse the strings
    for (string x : arr) {
 
        // Check the condition
        if (isDuogram(x, K)) {
            ans++;
        }
    }
 
    return ans;
}
 
// Driver Code
int main()
{
    vector arr = { "abab", "derdee", "erre" };
    int K = 2;
 
    cout << countDuograms(arr, K);
}


Java
// Java program for the above approach
class GFG{
 
// Function to check if a String
// is an duogram or not
static boolean isDuogram(String s, int K)
{
 
    // Array to store the frequency
    // of characters
   int []freq = new int[26];
 
    // Count the frequency
    for (char c : s.toCharArray()) {
        freq++;
    }
 
    // Check if the frequency is greater
    // than K or not
    for (int i = 0; i < 26; i++)
        if (freq[i] > K)
            return false;
 
    return true;
}
 
// Function to check if array arr contains
// all duograms or not
static int countDuograms(String[] arr, int K)
{
 
    // Store the answer
    int ans = 0;
 
    // Traverse the Strings
    for (String x : arr) {
 
        // Check the condition
        if (isDuogram(x, K)) {
            ans++;
        }
    }
 
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    String []arr = { "abab", "derdee", "erre" };
    int K = 2;
 
    System.out.print(countDuograms(arr, K));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python program for the above approach
 
# Function to check if a string
# is an duogram or not
def isDuogram (s, K) :
    # Array to store the frequency
    # of characters
    freq = [0] * 26
 
    # Count the frequency
    for c in s:
        freq[ord(c) - ord("a")] += 1
 
    # Check if the frequency is greater
    # than K or not
    for i in range(26):
        if (freq[i] > K):
            return False
    return True
 
 
# Function to check if array arr contains
# all duograms or not
def countDuograms  (arr, K) :
   
    # Store the answer
    ans = 0
    # Traverse the strings
    for x in arr:
        # Check the condition
        if (isDuogram(x, K)):
            ans += 1
    return ans
 
 
# Driver Code
arr = ["abab", "derdee", "erre"]
K = 2
 
print(countDuograms(arr, K))
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program for the above approach
using System;
class GFG
{
 
    // Function to check if a String
    // is an duogram or not
    static bool isDuogram(String s, int K)
    {
 
        // Array to store the frequency
        // of characters
        int[] freq = new int[26];
 
        // Count the frequency
        foreach (char c in s.ToCharArray())
        {
            freq[(int)c - (int)'a']++;
        }
 
        // Check if the frequency is greater
        // than K or not
        for (int i = 0; i < 26; i++)
            if (freq[i] > K)
                return false;
 
        return true;
    }
 
    // Function to check if array arr contains
    // all duograms or not
    static int countDuograms(String[] arr, int K)
    {
 
        // Store the answer
        int ans = 0;
 
        // Traverse the Strings
        foreach (String x in arr)
        {
 
            // Check the condition
            if (isDuogram(x, K))
            {
                ans++;
            }
        }
 
        return ans;
    }
 
    // Driver Code
    public static void Main()
    {
        String[] arr = { "abab", "derdee", "erre" };
        int K = 2;
 
        Console.Write(countDuograms(arr, K));
    }
}
 
// This code is contributed by gfgking


Javascript


输出
2

时间复杂度: O(N*M),其中 N 是数组的大小,M 是最长字符串的大小
辅助空间: O(1)