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 = 3Output:2Explanation:

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 = 7Output:2Explanation:

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++

`// C++ implementation to count ` `// the number of strings in the ` `// array whose distinct characters ` `// is less than or equal to M ` `#include <bits/stdc++.h> ` `#include <set> ` `using` `namespace` `std; ` ` ` `// Function to count the strings ` `// whose distinct characters ` `// count is less than M ` `void` `distinct(string S[], ` `int` `M, ` `int` `n) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `// Loop to iterate over all ` ` ` `// the strings of the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` ` ` `// Distinct characters in the ` ` ` `// String with the help of set ` ` ` `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(); ` ` ` ` ` `// Checking if its less ` ` ` `// than or equal to M ` ` ` `if` `(c <= M) ` ` ` `count += 1; ` ` ` `} ` ` ` `cout << (count); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `string S[] = { ` `"HERBIVORES"` `, ` `"AEROPLANE"` `, ` ` ` `"GEEKSFORGEEKS"` `}; ` ` ` `int` `M = 7; ` ` ` `int` `n = ` `sizeof` `(S) / ` `sizeof` `(S[0]); ` ` ` ` ` `distinct(S, M, n); ` ` ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to count ` `// the number of strings in the ` `// array whose distinct characters ` `// is less than or equal to M ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to count the strings ` `// whose distinct characters ` `// count is less than M ` `public` `static` `void` `distinct(String[] S, ` `int` `M) ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `// Loop to iterate over all ` ` ` `// the strings of the array ` ` ` `for` `(` `int` `i = ` `0` `; i < S.length; i++) ` ` ` `{ ` ` ` ` ` `// Distinct characters in the ` ` ` `// String with the help of set ` ` ` `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(); ` ` ` ` ` `// Checking if its less ` ` ` `// than or equal to M ` ` ` `if` `(c <= M) ` ` ` `count += ` `1` `; ` ` ` `} ` ` ` `System.out.println(count); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `String S[] = { ` `"HERBIVORES"` `, ` `"AEROPLANE"` `, ` ` ` `"GEEKSFORGEEKS"` `}; ` ` ` `int` `M = ` `7` `; ` ` ` ` ` `distinct(S, M); ` `} ` `} ` ` ` `// This code is contributed by jrishabh99 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to count ` `# the number of strings in the ` `# array whose distinct characters ` `# is less than or equal to M ` ` ` `# Function to count the strings ` `# whose distinct characters ` `# count is less than M ` `def` `distinct(S, M): ` ` ` `count ` `=` `0` ` ` ` ` `# Loop to iterate over all ` ` ` `# the strings of the array ` ` ` `for` `i ` `in` `range` `(` `len` `(S)): ` ` ` ` ` `# Distinct characters in the ` ` ` `# String with the help of set ` ` ` `c ` `=` `len` `(` `set` `([d ` `for` `d ` `in` `S[i]])) ` ` ` ` ` `# Checking if its less ` ` ` `# than or equal to M ` ` ` `if` `(c <` `=` `M): ` ` ` `count ` `+` `=` `1` ` ` `print` `(count) ` ` ` `# Driver Code ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` ` ` `S ` `=` `[` `"HERBIVORES"` `, ` `"AEROPLANE"` `, ` ` ` `"GEEKSFORGEEKS"` `] ` ` ` `M ` `=` `7` ` ` ` ` `distinct(S, M) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to count ` `// the number of strings in the ` `// array whose distinct characters ` `// is less than or equal to M ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG{ ` ` ` `// Function to count the strings ` `// whose distinct characters ` `// count is less than M ` `public` `static` `void` `distinct(` `string` `[] S, ` `int` `M) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `// Loop to iterate over all ` ` ` `// the strings of the array ` ` ` `for` `(` `int` `i = 0; i < S.Length; i++) ` ` ` `{ ` ` ` ` ` `// Distinct characters in the ` ` ` `// String with the help of set ` ` ` `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; ` ` ` ` ` `// Checking if its less ` ` ` `// than or equal to M ` ` ` `if` `(c <= M) ` ` ` `count += 1; ` ` ` `} ` ` ` `Console.Write(count); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(` `string` `[] args) ` `{ ` ` ` `string` `[]S = { ` `"HERBIVORES"` `, ` `"AEROPLANE"` `, ` ` ` `"GEEKSFORGEEKS"` `}; ` ` ` `int` `M = 7; ` ` ` ` ` `distinct(S, M); ` `} ` `} ` ` ` `// This code is contributed by rutvik_56 ` |

*chevron_right*

*filter_none*

**Output:**

2

**Performance Analysis:**

**Time Complexity:**O(N * M), where M is the maximum length of string.**Auxiliary Space:**O(1)

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.

## Recommended Posts:

- Count the number of words having sum of ASCII values less than and greater than k
- Sort an array of strings based on count of distinct characters
- Count of alphabets having ASCII value less than and greater than k
- Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Count of Distinct strings possible by inserting K characters in the original string
- Count ways to partition a string such that both parts have equal distinct characters
- Replace minimal number of characters to make all characters pair wise distinct
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Find the count of sub-strings whose characters can be rearranged to form the given word
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Count of substrings whose Decimal equivalent is greater than or equal to K
- Find distinct characters in distinct substrings of a string
- Find the number of strings formed using distinct characters of a given string
- Remove characters from string that appears strictly less than K times
- Longest suffix such that occurrence of each character is less than N after deleting atmost K characters
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Longest sub-string having frequency of each character less than equal to k

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.