Open In App

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

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. 

Below is the implementation of the above approach.




// 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 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




# 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# 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




<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).

 


Article Tags :