 GFG App
Open App Browser
Continue

# Count of strings with frequency of each character at most X and length at least Y

Given an array arr[] of strings and integers X and Y, the task is to find the count of strings with frequency of each character at most X and length of the string at least Y.

Examples:

Input: arr[] = { “ab”, “derdee”, “erre” }, X = 2, Y = 4
Output: 1
Explanation: Strings with character frequency at most 2 and
length at least 4 is “erre”. Hence count is 1

Input: arr[] = {“ag”, “ka”, “nanana”}, X = 3, Y = 2
Output: 3

Approach: Follow the approach mentioned below to solve the problem:

• Traverse the array of string, and for each string follow the steps below.
• Create a frequency map of characters.
• Whenever any character has a frequency greater than X, or length less than Y, skip the current string.
• If no character has frequency more than X, and length at least Y, increment the count of answer.
• Return the count stored in answer when all the strings are traversed.

## 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<``int``> 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``;` `    `  `    ``// 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

 ``

Output

`1`

Time Complexity: O(N*M), where N is the size of the array and M is the size of the longest string
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up