Count of isogram strings in given array of strings with length at least K
Last Updated :
16 Dec, 2021
Given an array arr[] containing N strings and an integer K, the task is to find the number of strings which are isograms and at least of length K.
Examples:
Input: arr[] = {"abcd", "der", "erty"}, K = 4
Output: 2
Explanation: All given strings are isograms, but only "abcd" and "erty" are of length at least K. Hence count is 2
Input: arr[] = {"ag", "bka", "lkmn", "asdfg"}, K = 2
Output: 4
Explanation: All the strings are isograms and each strings is of length >=K. Hence count is 4.
Approach: This problem can be solved by storing frequencies of all the characters or by using a Set data structure. A string is an isogram if no letter in that string appears more than once. Follow the steps below to solve the given problem.
- Traverse the array of strings arr[], and for each string
- Create a frequency map of characters.
- Wherever any character has a frequency greater than 1, or if the length of the string is less than K, skip the current string.
- Otherwise, if no character has a frequency more than 1, and if the length of the string is less than K, increment the count of the answer.
- Print the count stored in the answer when all the strings are traversed.
Below is the implementation of the above approach.
C++
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if a string
// is an isogram or not
bool isIsogram(string s)
{
// To store the frequencies
vector<int> freq(26, 0);
for (char c : s) {
freq[c - 'a']++;
if (freq[c - 'a'] > 1) {
return false;
}
}
return true;
}
// Function to check if array arr contains
// all isograms or not
int allIsograms(vector<string>& arr, int K)
{
int ans = 0;
for (string x : arr) {
if (isIsogram(x) && x.length() >= K) {
ans++;
}
}
return ans;
}
// Driver Code
int main()
{
vector<string> arr = { "abcd", "der", "erty" };
int K = 4;
// Function call and printing the answer
cout << allIsograms(arr, K);
}
Java
// Java code for the above approach
import java.io.*;
class GFG {
// Function to check if a string
// is an isogram or not
static boolean isIsogram(String s)
{
// To store the frequencies
int[] freq = new int[26];
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
freq[c - 'a']++;
if (freq[c - 'a'] > 1) {
return false;
}
}
return true;
}
// Function to check if array arr contains
// all isograms or not
static int allIsograms(String[] arr, int K)
{
int ans = 0;
for (String x : arr) {
if (isIsogram(x) && x.length() >= K) {
ans++;
}
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
String arr[] = { "abcd", "der", "erty" };
int K = 4;
// Function call and printing the answer
System.out.println(allIsograms(arr, K));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python code for the above approach
# Function to check if a string
# is an isogram or not
def isIsogram (s):
# To store the frequencies
freq = [0] * 26
for c in s:
freq[ord(c) - ord("a")] += 1
if (freq[s.index(c)] > 1):
return False
return True
# Function to check if array arr contains
# all isograms or not
def allIsograms (arr, K):
ans = 0
for x in arr:
if isIsogram(x) and len(x) >= K:
ans += 1
return ans
# Driver Code
arr = ["abcd", "der", "erty"]
K = 4
# Function call and printing the answer
print(allIsograms(arr, K))
# This code is contributed by Saurabh jaiswal
C#
// C# code for the above approach
using System;
class GFG{
// Function to check if a string
// is an isogram or not
static bool isIsogram(string s)
{
// To store the frequencies
int[] freq = new int[26];
for(int i = 0; i < s.Length; i++)
{
char c = s[i];
freq[c - 'a']++;
if (freq[c - 'a'] > 1)
{
return false;
}
}
return true;
}
// Function to check if array arr contains
// all isograms or not
static int allIsograms(string[] arr, int K)
{
int ans = 0;
foreach(string x in arr)
{
if (isIsogram(x) && x.Length >= K)
{
ans++;
}
}
return ans;
}
// Driver Code
public static void Main(string[] args)
{
string[] arr = { "abcd", "der", "erty" };
int K = 4;
// Function call and printing the answer
Console.WriteLine(allIsograms(arr, K));
}
}
// This code is contributed by ukasp
JavaScript
<script>
// JavaScript code for the above approach
// Function to check if a string
// is an isogram or not
const isIsogram = (s) => {
// To store the frequencies
let freq = new Array(26).fill(0);
for (let c in s) {
freq[s.charCodeAt(c) - "a".charCodeAt(0)]++;
if (freq[c] > 1) {
return false;
}
}
return true;
}
// Function to check if array arr contains
// all isograms or not
const allIsograms = (arr, K) => {
let ans = 0;
for (let x in arr) {
if (isIsogram(x) && arr[x].length >= K) {
ans++;
}
}
return ans;
}
// Driver Code
let arr = ["abcd", "der", "erty"];
let K = 4;
// Function call and printing the answer
document.write(allIsograms(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 Isogram strings in given Array of Strings Given an array arr[] containing N strings, the task is to find the count of strings which are isograms. A string is an isogram if no letter in that string appears more than once. Examples: Input: arr[] = {"abcd", "derg", "erty"}Output: 3Explanation: All given strings are isograms. In all the strings
5 min read
Count of strings whose prefix match with the given string to a given length k Given an array of strings arr[] and given some queries where each query consists of a string str and an integer k. The task is to find the count of strings in arr[] whose prefix of length k matches with the k length prefix of str.Examples: Input: arr[] = {"abba", "abbb", "abbc", "abbd", "abaa", "abc
15+ min read
Count of strings that does not contain any character of a given string Given an array arr containing N strings and a string str, the task is to find the number of strings that do not contain any character of string str. Examples: Input: arr[] = {"abcd", "hijk", "xyz", "ayt"}, str="apple"Output: 2Explanation: "hijk" and "xyz" are the strings that do not contain any char
8 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
Counting K-Length Strings with Fixed Character in a Unique String Given a string S of length n containing distinct characters and a character C , the task is to count k-length strings that can be formed using characters from the string S, ensuring each string includes the specified character C, and no characters from the given string S are used more than once. Ret
9 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