Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Count of isogram strings in given array of strings with length at least K

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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++;
 
        if (freq > 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++;
 
            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 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++;
 
        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;
    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 > 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>


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
Last Updated : 16 Dec, 2021
Like Article
Save Article
Similar Reads
Related Tutorials