Count of strings with frequency of each character at most X and length at least Y
Last Updated :
07 Dec, 2021
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 <bits/stdc++.h>
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[c - 'a']++;
}
// 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<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
int main()
{
vector<string> 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<s.length();i++) {
char c = s.charAt(i);
freq[c - 'a']++;
}
// 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
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[c - 'a']++;
}
// 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
<script>
// Function to check if
// the string has
// frequency of each character
// less than X
const isValid = (s, X) => {
let freq = new Array(26).fill(0);
// Loop to check the frequency
// of each character in the string
for (let c in s) {
freq[s.charCodeAt(c) - "a".charCodeAt(0)]++;
}
// Loop to check
// if the frequency of all characters
// are at most X
for (let i = 0; i < 26; i++)
if (freq[i] > X)
return false;
return true;
}
// Function to calculate the count of strings
const getCount = (arr, X, Y) => {
let ans = 0;
// Loop to iterate the string array
for (let st in arr) {
if (isValid(arr[st], X) && arr[st].length >= Y) {
ans++;
}
}
return ans;
}
// Driver Code
let arr = ["ab", "derdee", "erre"];
let X = 2, Y = 4;
// Function call to get count for arr[]
document.write(getCount(arr, X, Y));
// 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 strings with frequency of each character at most K 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 = 2Output: 2Explanation: Strings with character frequency at most 2 are "abab", "erre". Hence c
6 min read
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 substrings with the frequency of at most one character as Odd Given a string S of N characters, the task is to calculate the total number of non-empty substrings such that at most one character occurs an odd number of times. Example: Input: S = "aba"Output: 4Explanation: The valid substrings are "a", "b", "a", and "aba". Therefore, the total number of required
7 min read
Count substrings having frequency of a character exceeding that of another character in a string Given a string S of size N consisting of characters a, b, and c only, the task is to find the number of substrings of the given string S such that the frequency of character a is greater than the frequency of character c. Examples: Input: S = "abcc"Output: 2Explanation:Below are all the possible sub
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