Count of Isogram strings in given Array of Strings

• Last Updated : 16 Dec, 2021

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: 3
Explanation: All given strings are isograms. In all the strings no character
is present more than once. Hence count is 3

Input: arr[] = {“agka”, “lkmn”}
Output: 1
Explanation: Only string “lkmn” is isogram. In the string “agka”
the character ‘a’ is present twice. Hence count is 1.

Approach: Greedy approach can be used for solving this problem. Traverse each string in the given string array and check if that is isogram or not. To do that follow the steps mentioned below:

• Traverse the array of string and follow the below steps for each string:
• Create a frequency map of characters.
• Wherever any character has a frequency greater than 1, skip the current string and move to the next one.
• If no character has frequency more than 1, increment the count of answer by 1.
• Return the count stored in answer when all the strings are traversed.

Below is the implementation of the above approach:

C++

 #include using namespace std; // Function to check// if a string is an isogrambool isIsogram(string s){    // Loop to check    // if string is isogram or not    vector freq(26, 0);    for (char c : s) {        freq++;        if (freq > 1) {            return false;        }    }    return true;} // Function to count the number of isogramsint countIsograms(vector& arr){    int ans = 0;     // Loop to iterate the string array    for (string x : arr) {        if (isIsogram(x)) {            ans++;        }    }    return ans;} // Driver Codeint main(){    vector arr = { "abcd", "derg", "erty" };     // Count of isograms in string array arr[]    cout << countIsograms(arr) << endl;    return 0;}

Java

 // Java program for the above approachimport java.util.ArrayList; class GFG {     // Function to check    // if a String is an isogram    static boolean isIsogram(String s) {         // Loop to check        // if String is isogram or not        int[] freq = new int;        for (int i = 0; i < 26; i++) {            freq[i] = 0;        }         for (char c : s.toCharArray()) {            freq++;            if (freq > 1) {                return false;            }        }        return true;    }     // Function to count the number of isograms    static int countIsograms(ArrayList arr) {        int ans = 0;         // Loop to iterate the String array        for (String x : arr) {            if (isIsogram(x)) {                ans++;            }        }        return ans;    }     // Driver Code    public static void main(String args[]) {        ArrayList arr = new ArrayList();         arr.add("abcd");        arr.add("derg");        arr.add("erty");         // Count of isograms in String array arr[]        System.out.println(countIsograms(arr));    }} // This code is contributed by gfgking

Python3

 # Function to check# if a string is an isogramdef isIsogram(s):     # Loop to check    # if string is isogram or not    freq = *(26)    for c in s:        freq[ord(c) - ord('a')] += 1        if (freq[ord(c) - ord('a')] > 1):            return False     return True # Function to count the number of isogramsdef countIsograms(arr):    ans = 0     # Loop to iterate the string array    for x in arr:        if (isIsogram(x)):            ans += 1     return ans # Driver Codeif __name__ == "__main__":     arr = ["abcd", "derg", "erty"]     # Count of isograms in string array arr[]    print(countIsograms(arr))     # This code is contributed by ukasp.

C#

 // C# program for the above approachusing System;using System.Collections; class GFG{     // Function to check// if a string is an isogramstatic bool isIsogram(string s){       // Loop to check    // if string is isogram or not    int []freq = new int;    for(int i = 0; i < 26; i++) {        freq[i] = 0;    }         foreach (char c in s) {        freq++;        if (freq > 1) {            return false;        }    }    return true;} // Function to count the number of isogramsstatic int countIsograms(ArrayList arr){    int ans = 0;     // Loop to iterate the string array    foreach (string x in arr) {        if (isIsogram(x)) {            ans++;        }    }    return ans;} // Driver Codepublic static void Main(){    ArrayList arr = new ArrayList();         arr.Add("abcd");    arr.Add("derg");    arr.Add("erty");     // Count of isograms in string array arr[]    Console.WriteLine(countIsograms(arr));}} // This code is contributed by Samim Hossain Mondal.

Javascript



Output
3

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