# Count of strings with frequency of each character at most K

• Last Updated : 06 Dec, 2021

Given an array arr[] containing N strings and an integer K, the task is to find the count of strings with the frequency of each character at most K

Examples:

Input: arr[] = { “abab”, “derdee”, “erre” }, K = 2
Output: 2
Explanation: Strings with character frequency at most 2 are “abab”, “erre”. Hence count is 2

Input: arr[] = {“ag”, “ka”, “nanana”}, K = 3
Output: 1

Approach: The idea is to traverse the array of strings, and for each string create a frequency map of characters. Wherever any character has a frequency greater than K, skip the current string. Otherwise, if no character has a frequency more than K, increment the count of answer. Print the count stored in the answer when all the strings are traversed. Follow the steps below to solve the problem:

• Define a function isDuogram(string s, int K) and perform the following tasks:
• Initialize the vector freq[26] with values 0.
• Traverse over the string s using the variable c and perform the following tasks:
• Increase the count of freq by 1.
• Iterate over the range [0. 26) using the variable i and perform the following tasks:
• If freq[i] is greater than K then return false.
• After performing the above steps, return true as the answer.
• Initialize the variable ans as 0 to store the answer.
• Traverse over the array arr[] using the variable x and perform the following tasks:
• Call the function isDuogram(x, K) and if the function returns true then increase the count of ans by 1.
• After performing the above steps, print the value of ans as the answer.

Below is the implementation of the above approach:

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

 ``
Output
`2`

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