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 <bits/stdc++.h>
using namespace std;
bool isIsogram(string s)
{
vector< int > freq(26, 0);
for ( char c : s) {
freq++;
if (freq > 1) {
return false ;
}
}
return true ;
}
int countIsograms(vector<string>& arr)
{
int ans = 0;
for (string x : arr) {
if (isIsogram(x)) {
ans++;
}
}
return ans;
}
int main()
{
vector<string> arr = { "abcd" , "derg" , "erty" };
cout << countIsograms(arr) << endl;
return 0;
}
|
Java
import java.util.ArrayList;
class GFG {
static boolean isIsogram(String s) {
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 ;
}
static int countIsograms(ArrayList<String> arr) {
int ans = 0 ;
for (String x : arr) {
if (isIsogram(x)) {
ans++;
}
}
return ans;
}
public static void main(String args[]) {
ArrayList<String> arr = new ArrayList<String>();
arr.add( "abcd" );
arr.add( "derg" );
arr.add( "erty" );
System.out.println(countIsograms(arr));
}
}
|
Python3
def isIsogram(s):
freq = [ 0 ] * ( 26 )
for c in s:
freq[ ord (c) - ord ( 'a' )] + = 1
if (freq[ ord (c) - ord ( 'a' )] > 1 ):
return False
return True
def countIsograms(arr):
ans = 0
for x in arr:
if (isIsogram(x)):
ans + = 1
return ans
if __name__ = = "__main__" :
arr = [ "abcd" , "derg" , "erty" ]
print (countIsograms(arr))
|
C#
using System;
using System.Collections;
class GFG
{
static bool isIsogram( string s)
{
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 ;
}
static int countIsograms(ArrayList arr)
{
int ans = 0;
foreach ( string x in arr) {
if (isIsogram(x)) {
ans++;
}
}
return ans;
}
public static void Main()
{
ArrayList arr = new ArrayList();
arr.Add( "abcd" );
arr.Add( "derg" );
arr.Add( "erty" );
Console.WriteLine(countIsograms(arr));
}
}
|
Javascript
<script>
const isIsogram = (s) => {
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 ;
}
const countIsograms = (arr) => {
let ans = 0;
for (let x in arr) {
if (isIsogram(x)) {
ans++;
}
}
return ans;
}
let arr = [ "abcd" , "derg" , "erty" ];
document.write(countIsograms(arr));
</script>
|
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)
Share your thoughts in the comments
Please Login to comment...