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
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++
#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 ;
}
bool allIsograms(vector<string>& arr)
{
for (string x : arr) {
if (!isIsogram(x)) {
return false ;
}
}
return true ;
}
int main()
{
vector<string> arr = { "abcd" , "derg" , "erty" };
if (allIsograms(arr)) {
cout << "Yes" ;
return 0;
}
cout << "No" ;
}
|
Java
import java.io.*;
class GFG {
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 ;
}
static boolean allIsograms(String arr[])
{
for (String x : arr) {
if (isIsogram(x) == false ) {
return false ;
}
}
return true ;
}
public static void main(String[] args)
{
String arr[] = { "abcd" , "derg" , "erty" };
if (allIsograms(arr) == true ) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
}
|
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 allIsograms (arr):
for x in arr:
if ( not isIsogram(x)):
return False
return True
arr = [ "abcd" , "derg" , "erty" ]
if (allIsograms(arr)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class GFG {
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 ;
}
static bool allIsograms(String []arr)
{
foreach (String x in arr) {
if (isIsogram(x) == false ) {
return false ;
}
}
return true ;
}
public static void Main(String[] args)
{
String []arr = { "abcd" , "derg" , "erty" };
if (allIsograms(arr) == true ) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
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 ;
}
const allIsograms = (arr) => {
for (let x in arr) {
if (!isIsogram(arr[x])) {
return false ;
}
}
return true ;
}
let arr = [ "abcd" , "derg" , "erty" ];
if (allIsograms(arr))
document.write( "Yes" );
else
document.write( "No" );
</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)
Last Updated :
01 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...