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[c 1="a" language="-"][/c] 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 <bits/stdc++.h>
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[c - '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
int countDuograms(vector<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
int main()
{
vector<string> 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[c - '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
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
<script>
// JavaScript program for the above approach
// Function to check if a string
// is an duogram or not
const isDuogram = (s, K) => {
// Array to store the frequency
// of characters
let freq = new Array(26).fill(0);
// Count the frequency
for (let c in s) {
freq[s.charCodeAt(c) - "a".charCodeAt(0)]++;
}
// Check if the frequency is greater
// than K or not
for (let i = 0; i < 26; i++)
if (freq[i] > K)
return false;
return true;
}
// Function to check if array arr contains
// all duograms or not
const countDuograms = (arr, K) => {
// Store the answer
let ans = 0;
// Traverse the strings
for (let x in arr) {
// Check the condition
if (isDuogram(arr[x], K)) {
ans++;
}
}
return ans;
}
// Driver Code
let arr = ["abab", "derdee", "erre"];
let K = 2;
document.write(countDuograms(arr, K));
// This code is contributed by rakeshsahni
</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)
Similar Reads
Count of substrings of given string with frequency of each character at most K Given a string str, the task is to calculate the number of substrings of the given string such that the frequency of each element of the string is almost K. Examples: Input: str = "abab", K = 1Output: 7Explanation: The substrings such that the frequency of each character is atmost 1 are "a", "b", "a
6 min read
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 = 4Output: 1Explanation: Strings with character frequency at mo
6 min read
Number of substrings with count of each character as k Given a string and an integer k, find the number of substrings in which all the different characters occur exactly k times. Examples: Input : s = "aabbcc" k = 2 Output : 6 The substrings are aa, bb, cc, aabb, bbcc and aabbcc. Input : s = "aabccc" k = 2 Output : 3 There are three substrings aa, cc an
15 min read
Count substrings with each character occurring at most k times Given a string S. Count number of substrings in which each character occurs at most k times. Assume that the string consists of only lowercase English alphabets. Examples: Input : S = ab k = 1 Output : 3 All the substrings a, b, ab have individual character count less than 1. Input : S = aaabb k = 2
15+ min read
Count of Substrings with at least K pairwise Distinct Characters having same Frequency Given a string S and an integer K, the task is to find the number of substrings which consists of at least K pairwise distinct characters having same frequency. Examples: Input: S = "abasa", K = 2 Output: 5 Explanation: The substrings in having 2 pairwise distinct characters with same frequency are
7 min read
Count of substrings of length K with exactly K-1 distinct characters Given a string s consisting of lowercase characters and an integer k, find the count of all substrings of length k which have exactly k-1 distinct characters.Example:Input: s = "aabab", k = 3 Output: 3Explanation: Substrings of length 3 are "aab", "aba", "bab". All 3 substrings contains 2 distinct c
8 min read