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 using namespace std; // Function to check if a string// is an isogram or notbool isIsogram(string s){    // To store the frequencies    vector freq(26, 0);     for (char c : s) {        freq++;         if (freq > 1) {            return false;        }    }     return true;} // Function to check if array arr contains// all isograms or notint allIsograms(vector& arr, int K){    int ans = 0;    for (string x : arr) {        if (isIsogram(x) && x.length() >= K) {            ans++;        }    }     return ans;} // Driver Codeint main(){    vector arr = { "abcd", "der", "erty" };    int K = 4;     // Function call and printing the answer    cout << allIsograms(arr, K);}

Java

 // Java code for the above approachimport 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;         for (int i = 0; i < s.length(); i++) {            char c = s.charAt(i);            freq++;             if (freq > 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 notdef isIsogram (s):     # To store the frequencies    freq =  * 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 notdef allIsograms (arr, K):    ans = 0    for x in arr:        if isIsogram(x) and len(x) >= K:            ans += 1     return ans  # Driver Codearr = ["abcd", "der", "erty"]K = 4 # Function call and printing the answerprint(allIsograms(arr, K)) # This code is contributed by Saurabh jaiswal

C#

 // C# code for the above approachusing System; class GFG{ // Function to check if a string// is an isogram or notstatic bool isIsogram(string s){         // To store the frequencies    int[] freq = new int;     for(int i = 0; i < s.Length; i++)    {        char c = s[i];        freq++;         if (freq > 1)        {            return false;        }    }    return true;} // Function to check if array arr contains// all isograms or notstatic int allIsograms(string[] arr, int K){    int ans = 0;    foreach(string x in arr)    {        if (isIsogram(x) && x.Length >= K)        {            ans++;        }    }    return ans;} // Driver Codepublic 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


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