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++
#include <bits/stdc++.h>
using namespace std;
bool isDuogram(string s, int K)
{
vector< int > freq(26, 0);
for ( char c : s) {
freq++;
}
for ( int i = 0; i < 26; i++)
if (freq[i] > K)
return false ;
return true ;
}
int countDuograms(vector<string>& arr, int K)
{
int ans = 0;
for (string x : arr) {
if (isDuogram(x, K)) {
ans++;
}
}
return ans;
}
int main()
{
vector<string> arr = { "abab" , "derdee" , "erre" };
int K = 2;
cout << countDuograms(arr, K);
}
|
Java
class GFG{
static boolean isDuogram(String s, int K)
{
int []freq = new int [ 26 ];
for ( char c : s.toCharArray()) {
freq++;
}
for ( int i = 0 ; i < 26 ; i++)
if (freq[i] > K)
return false ;
return true ;
}
static int countDuograms(String[] arr, int K)
{
int ans = 0 ;
for (String x : arr) {
if (isDuogram(x, K)) {
ans++;
}
}
return ans;
}
public static void main(String[] args)
{
String []arr = { "abab" , "derdee" , "erre" };
int K = 2 ;
System.out.print(countDuograms(arr, K));
}
}
|
Python3
def isDuogram (s, K) :
freq = [ 0 ] * 26
for c in s:
freq[ ord (c) - ord ( "a" )] + = 1
for i in range ( 26 ):
if (freq[i] > K):
return False
return True
def countDuograms (arr, K) :
ans = 0
for x in arr:
if (isDuogram(x, K)):
ans + = 1
return ans
arr = [ "abab" , "derdee" , "erre" ]
K = 2
print (countDuograms(arr, K))
|
C#
using System;
class GFG
{
static bool isDuogram(String s, int K)
{
int [] freq = new int [26];
foreach ( char c in s.ToCharArray())
{
freq[( int )c - ( int ) 'a' ]++;
}
for ( int i = 0; i < 26; i++)
if (freq[i] > K)
return false ;
return true ;
}
static int countDuograms(String[] arr, int K)
{
int ans = 0;
foreach (String x in arr)
{
if (isDuogram(x, K))
{
ans++;
}
}
return ans;
}
public static void Main()
{
String[] arr = { "abab" , "derdee" , "erre" };
int K = 2;
Console.Write(countDuograms(arr, K));
}
}
|
Javascript
<script>
const isDuogram = (s, K) => {
let freq = new Array(26).fill(0);
for (let c in s) {
freq[s.charCodeAt(c) - "a" .charCodeAt(0)]++;
}
for (let i = 0; i < 26; i++)
if (freq[i] > K)
return false ;
return true ;
}
const countDuograms = (arr, K) => {
let ans = 0;
for (let x in arr) {
if (isDuogram(arr[x], K)) {
ans++;
}
}
return ans;
}
let arr = [ "abab" , "derdee" , "erre" ];
let K = 2;
document.write(countDuograms(arr, K));
</script>
|
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)