📌  相关文章
📜  每个字符的频率最多为 X 且长度至少为 Y 的字符串计数

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

每个字符的频率最多为 X 且长度至少为 Y 的字符串计数

给定一个由字符串和整数XY组成的数组arr[] ,任务是找到每个字符的频率最多为 X且字符串长度至少为 Y的字符串计数。

例子:

方法:按照下面提到的方法解决问题:

  • 遍历字符串数组,对每个字符串执行以下步骤。
  • 创建字符的频率图
  • 每当任何字符的频率大于 X或长度小于 Y时,跳过当前字符串。
  • 如果没有字符的频率超过 X,长度至少为 Y,则增加答案计数
  • 当遍历所有字符串时,返回存储在 answer 中的计数。
C++
#include 
using namespace std;
 
// Function to check if
// the string has
// frequency of each character
// less than X
bool isValid(string s, int X)
{
    vector freq(26, 0);
 
    // Loop to check the frequency
    // of each character in the string
    for (char c : s) {
        freq++;
    }
 
    // Loop to check
    // if the frequency of all characters
    // are at most X
    for (int i = 0; i < 26; i++)
        if (freq[i] > X)
            return false;
    return true;
}
 
// Function to calculate the count of strings
int getCount(vector& arr, int X, int Y)
{
    int ans = 0;
 
    // Loop to iterate the string array
    for (string st : arr) {
        if (isValid(st, X) && st.length() >= Y) {
            ans++;
        }
    }
    return ans;
}
 
// Driver Code
int main()
{
    vector arr = { "ab", "derdee", "erre" };
    int X = 2, Y = 4;
 
    // Function call to get count for arr[]
    cout << getCount(arr, X, Y);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG
{
 
  // Function to check if
  // the string has
  // frequency of each character
  // less than X
  static boolean isValid(String s, int X)
  {
    int freq[] = new int[26];
 
    // Loop to check the frequency
    // of each character in the string
    for (int i=0;i X)
        return false;
    return true;
  }
 
  // Function to calculate the count of strings
  static int getCount(String[] arr, int X, int Y)
  {
    int ans = 0;
 
    // Loop to iterate the string array
    for (String st : arr) {
      if (isValid(st, X) && st.length() >= Y) {
        ans++;
      }
    }
    return ans;
  }
 
  // Driver Code
  public static void main (String[] args)
  {
    String arr[] = { "ab", "derdee", "erre" };
    int X = 2, Y = 4;
 
    // Function call to get count for arr[]
 
    System.out.println(getCount(arr, X, Y));
  }
}
 
// This code is contributed by Potta Lokesh


Python3
# Function to check if
# the string has
# frequency of each character
# less than X
def isValid (s, X) :
    freq = [0] * 26
 
    # Loop to check the frequency
    # of each character in the string
    for c in s:
        freq[ord(c)  - ord("a")] += 1
     
 
    # Loop to check
    # if the frequency of all characters
    # are at most X
    for i in range(26):
        if (freq[i] > X):
            return False
    return True
 
 
# Function to calculate the count of strings
def getCount (arr, X, Y):
    ans = 0
 
    # Loop to iterate the string array
    for st in arr:
        if (isValid(st, X) and len(st) >= Y):
            ans += 1
    return ans
 
 
# Driver Code
 
arr = ["ab", "derdee", "erre"]
X = 2
Y = 4
 
# Function call to get count for arr[]
print(getCount(arr, X, Y))
 
# This code is contributed by gfgking.


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if the string
// has frequency of each character
// less than X
static bool isValid(String s, int X)
{
    int []freq = new int[26];
     
    // Loop to check the frequency
    // of each character in the string
    for(int i = 0; i < s.Length; i++)
    {
        char c = s[i];
        freq++;
    }
     
    // Loop to check if the frequency
    // of all characters are at most X
    for(int i = 0; i < 26; i++)
        if (freq[i] > X)
            return false;
             
        return true;
}
 
// Function to calculate the count of strings
static int getCount(String[] arr, int X, int Y)
{
    int ans = 0;
     
    // Loop to iterate the string array
    foreach (String st in arr)
    {
        if (isValid(st, X) && st.Length >= Y)
        {
            ans++;
        }
    }
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
    String []arr = { "ab", "derdee", "erre" };
    int X = 2, Y = 4;
     
    // Function call to get count for []arr
    Console.WriteLine(getCount(arr, X, Y));
}
}
 
// This code is contributed by shikhasingrajput


Javascript


输出
1

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