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;
// 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[c - 'a']++;
if (freq[c - 'a'] > 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[c - 'a']++;
if (freq[c - 'a'] > 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[c - 'a']++;
if (freq[c - 'a'] > 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[c] > 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>
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)
Similar Reads
Count of Superstrings in a given array of strings Given 2 array of strings X and Y, the task is to find the number of superstrings in X. A string s is said to be a Superstring, if each string present in array Y is a subsequence of string s . Examples: Input: X = {"ceo", "alco", "caaeio", "ceai"}, Y = {"ec", "oc", "ceo"}Output: 2Explanation: Strings
8 min read
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 = 4Output: 2Explanation: All given strings are isograms, but only "abcd" and "erty" are of length at
5 min read
Count of Strings of Array having given prefixes for Q query Given two arrays of strings containing words[] and queries[] having N and Q strings respectively, the task is to find the number of strings from words[] having queries[i] as the prefix for all the strings in queries[]. Examples: Input: words[] = { "geeks", "geeks", "geeks for geeks", "string", "stro
13 min read
Frequency of a string in an array of strings You are given a collection of strings and a list of queries. For every query there is a string given. We need to print the number of times the given string occurs in the collection of strings. Examples: Input : arr[] = {wer, wer, tyu, oio, tyu} q[] = {wer, tyu, uio}Output : 2 2 0Explanation : q[0] a
15 min read
Check if all given strings are isograms or not Given an array arr containing N strings, the task is to check if all strings are isogram or not. If they are, print Yes, otherwise No. An Isogram is a word in which no letter occurs more than once. Examples: Input: arr[] = {"abcd", "derg", "erty"}Output: Yes Input: arr[] = {"agka", "lkmn"}Output: No
4 min read
Number of strings in two array satisfy the given conditions Given two arrays of string arr1[] and arr2[]. For each string in arr2[](say str2), the task is to count numbers string in arr1[](say str1) which satisfy the below conditions: The first characters of str1 and str2 must be equal.String str2 must contain each character of string str1.Examples: Input: a
14 min read