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 = 4
Output: 2
Explanation: All given strings are isograms, but only “abcd” and “erty” are of length at least K. Hence count is 2
Input: arr[] = {“ag”, “bka”, “lkmn”, “asdfg”}, K = 2
Output: 4
Explanation: All the strings are isograms and each strings is of length >=K. Hence count is 4.
Approach: This problem can be solved by storing frequencies of all the characters or by using a Set data structure. A string is an isogram if no letter in that string appears more than once. Follow the steps below to solve the given problem.
- Traverse the array of strings arr[], and for each string
- Create a frequency map of characters.
- Wherever any character has a frequency greater than 1, or if the length of the string is less than K, skip the current string.
- Otherwise, if no character has a frequency more than 1, and if the length of the string is less than K, increment the count of the answer.
- Print the count stored in the 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 allIsograms(vector<string>& arr, int K)
{
int ans = 0;
for (string x : arr) {
if (isIsogram(x) && x.length() >= K) {
ans++;
}
}
return ans;
}
int main()
{
vector<string> arr = { "abcd" , "der" , "erty" };
int K = 4;
cout << allIsograms(arr, K);
}
|
Java
import java.io.*;
class GFG {
static boolean isIsogram(String s)
{
int [] freq = new int [ 26 ];
for ( int i = 0 ; i < s.length(); i++) {
char c = s.charAt(i);
freq++;
if (freq > 1 ) {
return false ;
}
}
return true ;
}
static int allIsograms(String[] arr, int K)
{
int ans = 0 ;
for (String x : arr) {
if (isIsogram(x) && x.length() >= K) {
ans++;
}
}
return ans;
}
public static void main(String[] args)
{
String arr[] = { "abcd" , "der" , "erty" };
int K = 4 ;
System.out.println(allIsograms(arr, K));
}
}
|
Python3
def isIsogram (s):
freq = [ 0 ] * 26
for c in s:
freq[ ord (c) - ord ( "a" )] + = 1
if (freq[s.index(c)] > 1 ):
return False
return True
def allIsograms (arr, K):
ans = 0
for x in arr:
if isIsogram(x) and len (x) > = K:
ans + = 1
return ans
arr = [ "abcd" , "der" , "erty" ]
K = 4
print (allIsograms(arr, K))
|
C#
using System;
class GFG{
static bool isIsogram( string s)
{
int [] freq = new int [26];
for ( int i = 0; i < s.Length; i++)
{
char c = s[i];
freq++;
if (freq > 1)
{
return false ;
}
}
return true ;
}
static int allIsograms( string [] arr, int K)
{
int ans = 0;
foreach ( string x in arr)
{
if (isIsogram(x) && x.Length >= K)
{
ans++;
}
}
return ans;
}
public static void Main( string [] args)
{
string [] arr = { "abcd" , "der" , "erty" };
int K = 4;
Console.WriteLine(allIsograms(arr, K));
}
}
|
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 > 1) {
return false ;
}
}
return true ;
}
const allIsograms = (arr, K) => {
let ans = 0;
for (let x in arr) {
if (isIsogram(x) && arr[x].length >= K) {
ans++;
}
}
return ans;
}
let arr = [ "abcd" , "der" , "erty" ];
let K = 4;
document.write(allIsograms(arr, K));
</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 :
16 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...