Count of Isogram strings in given Array of Strings
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 3Input: 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 <bits/stdc++.h> using namespace std; // Function to check // if a string is an isogram bool 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 isograms int countIsograms(vector<string>& arr) { int ans = 0; // Loop to iterate the string array for (string x : arr) { if (isIsogram(x)) { ans++; } } return ans; } // Driver Code int main() { vector<string> arr = { "abcd" , "derg" , "erty" }; // Count of isograms in string array arr[] cout << countIsograms(arr) << endl; return 0; } |
Java
// Java program for the above approach import 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 |
Python3
# Function to check # if a string is an isogram def 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 isograms def countIsograms(arr): ans = 0 # Loop to iterate the string array for x in arr: if (isIsogram(x)): ans + = 1 return ans # Driver Code if __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 approach using System; using System.Collections; class GFG { // Function to check // if a string is an isogram static 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 isograms static 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 Code public 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
<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 string
Auxiliary Space: O(1)
Please Login to comment...