Count the number of strings in an array whose distinct characters are less than equal to M
Given an array of strings arr[] and an integer M, the task is to count the strings whose count of distinct characters is less than M.
Examples:
Input: arr[] = {“ADAM”, “JOHNSON”, “COOL”}, M = 3
Output: 2
Explanation:
There are two such strings whose count of distinct characters is less than M.
Count of Distinct(“ADAM”) = 3
Count of Distinct(“COOL”) = 3
Input: arr[] = {“HERBIVORES”, “AEROPLANE”, “GEEKSFORGEEKS”}, M = 7
Output: 2
Explanation:
There are two such strings whose count of distinct characters is less than M.
Count of Distinct(“AEROPLANE”) = 7
Count of Distinct(“GEEKSFORGEEKS”) = 7
Approach: The idea is to iterate over all the strings and find the distinct characters of the string, If the count of the distinct characters in the string is less than or equal to the given value of the M, then increment the count by 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <set>
using namespace std;
void distinct(string S[], int M, int n)
{
int count = 0;
for ( int i = 0; i < n; i++)
{
set< char > set1;
for ( int j = 0; j < S[i].length(); j++)
{
if (set1.find(S[i][j]) == set1.end())
set1.insert(S[i][j]);
}
int c = set1.size();
if (c <= M)
count += 1;
}
cout << (count);
}
int main()
{
string S[] = { "HERBIVORES" , "AEROPLANE" ,
"GEEKSFORGEEKS" };
int M = 7;
int n = sizeof (S) / sizeof (S[0]);
distinct(S, M, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static void distinct(String[] S, int M)
{
int count = 0 ;
for ( int i = 0 ; i < S.length; i++)
{
Set<Character> set = new HashSet<>();
for ( int j = 0 ; j < S[i].length(); j++)
{
if (!set.contains(S[i].charAt(j)))
set.add(S[i].charAt(j));
}
int c = set.size();
if (c <= M)
count += 1 ;
}
System.out.println(count);
}
public static void main(String[] args)
{
String S[] = { "HERBIVORES" , "AEROPLANE" ,
"GEEKSFORGEEKS" };
int M = 7 ;
distinct(S, M);
}
}
|
Python3
def distinct(S, M):
count = 0
for i in range ( len (S)):
c = len ( set ([d for d in S[i]]))
if (c < = M):
count + = 1
print (count)
if __name__ = = '__main__' :
S = [ "HERBIVORES" , "AEROPLANE" ,
"GEEKSFORGEEKS" ]
M = 7
distinct(S, M)
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static void distinct( string [] S, int M)
{
int count = 0;
for ( int i = 0; i < S.Length; i++)
{
HashSet< char > set = new HashSet< char >();
for ( int j = 0; j < S[i].Length; j++)
{
if (! set .Contains(S[i][j]))
set .Add(S[i][j]);
}
int c = set .Count;
if (c <= M)
count += 1;
}
Console.Write(count);
}
public static void Main( string [] args)
{
string []S = { "HERBIVORES" , "AEROPLANE" ,
"GEEKSFORGEEKS" };
int M = 7;
distinct(S, M);
}
}
|
Javascript
<script>
function distinct(S, M, n) {
let count = 0;
for (let i = 0; i < n; i++) {
let set1 = new Set();
for (let j = 0; j < S[i].length; j++) {
if (!set1.has(S[i][j]))
set1.add(S[i][j]);
}
let c = set1.size;
if (c <= M)
count += 1;
}
document.write(count);
}
let S = [ "HERBIVORES" , "AEROPLANE" ,
"GEEKSFORGEEKS" ];
let M = 7;
let n = S.length;
distinct(S, M, n);
</script>
|
Performance Analysis:
- Time Complexity: O(N * M*logM), where M is the maximum length of the string.
- Auxiliary Space: O(M)
Last Updated :
16 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...