Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Check if all given strings are isograms or not

  • Last Updated : 01 Dec, 2021

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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Examples:



Input: arr[] = {“abcd”, “derg”, “erty”}
Output: Yes

Input: arr[] = {“agka”, “lkmn”}
Output: No

 

Approach: A string is an isogram if no letter in that string appears more than once. Now to solve this problem, 

  • Traverse the array arr, and for each string
  • Create a frequency map of characters.
  • Wherever any character has a frequency greater than 1, print No and return.
  • Otherwise, after the whole array is traversed, print Yes.

Below is the implementation of the above approach:

C++




// C++ code for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if a string
// is an isogram or not
bool isIsogram(string s)
{
    vector<int> freq(26, 0);
    for (char c : s) {
        freq++;
        if (freq > 1) {
            return false;
        }
    }
 
    return true;
}
 
// Function to check if array arr contains
// all isograms or not
bool allIsograms(vector<string>& arr)
{
    for (string x : arr) {
        if (!isIsogram(x)) {
            return false;
        }
    }
 
    return true;
}
 
// Driver Code
int main()
{
    vector<string> arr = { "abcd", "derg", "erty" };
    if (allIsograms(arr)) {
        cout << "Yes";
        return 0;
    }
    cout << "No";
}


Java




// Java program for the above approach
import java.io.*;
 
class GFG {
 
    // Function to check if a string
    // is an isogram or not
    static boolean isIsogram(String s)
    {
        int freq[] = new int[26];
        char S[] = s.toCharArray();
        for (char c : S) {
            freq++;
            if (freq > 1) {
                return false;
            }
        }
 
        return true;
    }
 
    // Function to check if array arr contains
    // all isograms or not
    static boolean allIsograms(String arr[])
    {
        for (String x : arr) {
            if (isIsogram(x) == false) {
                return false;
            }
        }
 
        return true;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String arr[] = { "abcd", "derg", "erty" };
        if (allIsograms(arr) == true) {
            System.out.println("Yes");
        }
        else {
            System.out.println("No");
        }
    }
}
 
 // This code is contributed by Potta Lokesh


Python3




# Python code for the above approach
 
# Function to check if a string
# is an isogram or not
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
 
# Function to check if array arr contains
# all isograms or not
def allIsograms (arr):
    for x in arr:
        if (not isIsogram(x)):
            return False
    return True
 
# Driver Code
arr = ["abcd", "derg", "erty"]
if (allIsograms(arr)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by Saurabh Jaiswal


C#




// C# program for the above approach
using System;
 
public class GFG {
 
    // Function to check if a string
    // is an isogram or not
    static bool isIsogram(String s)
    {
        int []freq = new int[26];
        char []S = s.ToCharArray();
        foreach (char c in S) {
            freq++;
            if (freq > 1) {
                return false;
            }
        }
 
        return true;
    }
 
    // Function to check if array arr contains
    // all isograms or not
    static bool allIsograms(String []arr)
    {
        foreach (String x in arr) {
            if (isIsogram(x) == false) {
                return false;
            }
        }
 
        return true;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        String []arr = { "abcd", "derg", "erty" };
        if (allIsograms(arr) == true) {
            Console.WriteLine("Yes");
        }
        else {
            Console.WriteLine("No");
        }
    }
}
 
// This code is contributed by 29AjayKumar


Javascript




<script>
    // JavaScript code for the above approach
 
    // Function to check if a string
    // is an isogram or not
    const isIsogram = (s) => {
        let freq = new Array(26).fill(0);
        for (let c in s) {
            freq[s.charCodeAt(c) - 'a'.charCodeAt(0)]++;
            if (freq[s.charCodeAt(c) - 'a'.charCodeAt(0)] > 1) {
                return false;
            }
        }
 
        return true;
    }
 
    // Function to check if array arr contains
    // all isograms or not
    const allIsograms = (arr) => {
        for (let x in arr) {
            if (!isIsogram(arr[x])) {
                return false;
            }
        }
 
        return true;
    }
 
    // Driver Code
    let arr = ["abcd", "derg", "erty"];
    if (allIsograms(arr))
        document.write("Yes");
    else
        document.write("No");
 
    // This code is contributed by rakeshsahni
 
</script>


 
 

Output

Yes

 

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
Recommended Articles
Page :

Start Your Coding Journey Now!