Count of Isogram strings in given Array of Strings - GeeksforGeeks

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: 3Explanation: 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: 1Explanation: 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++
Java
Python3
C#
Javascript
#include <bits/stdc++.h>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<int> freq(26, 0);    for (char c : s) {        freq++;        if (freq > 1) {            return false;        }    }    return true;} // Function to count the number of isogramsint countIsograms(vector<string>& arr){    int ans = 0;     // Loop to iterate the string array    for (string x : arr) {        if (isIsogram(x)) {            ans++;        }    }    return ans;} // Driver Codeint main(){    vector<string> arr = { "abcd", "derg", "erty" };     // Count of isograms in string array arr[]    cout << countIsograms(arr) << endl;    return 0;}
// 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[26];        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<String> 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<String> arr = new ArrayList<String>();         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
# Function to check# if a string is an isogramdef isIsogram(s):     # Loop to check    # if string is isogram or not    freq = [0]*(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# 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[26];    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.
<script>     // Function to check    // if a string is an isogram    const isIsogram = (s) => {             // Loop to check        // if string is isogram or not        let freq = new Array(26).fill(0);        for (let c in s) {            freq[s.charCodeAt(c) - "0".charCodeAt(0)]++;            if (freq > 1) {                return false;            }        }        return true;    }     // Function to count the number of isograms    const countIsograms = (arr) => {        let ans = 0;         // Loop to iterate the string array        for (let x in arr) {            if (isIsogram(x)) {                ans++;            }        }        return ans;    }     // Driver Code    let arr = ["abcd", "derg", "erty"];     // Count of isograms in string array arr[]    document.write(countIsograms(arr));     // This code is contributed by rakeshsahni </script>
  
3
 
Time Complexity: O(N*M), where N is the size of the array and M is the size of the longest stringAuxiliary Space: O(1)
 
rakeshsahni
samim2000
ukasp
gfgking
frequency-counting
Arrays
Hash
Strings
Arrays
Hash
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program for Bridge and Torch problem
Window Sliding Technique
Binary Tree (Array implementation)
Segment Tree | Set 1 (Sum of given range)
MSD( Most Significant Digit ) Radix Sort
Internal Working of HashMap in Java
Count pairs with given sum
Hashing | Set 1 (Introduction)
Hashing | Set 3 (Open Addressing)
Hashing | Set 2 (Separate Chaining)