给定一个字符串数组arr[]和一个整数M ,任务是计算不同字符数小于M的字符串。
例子:
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
方法:我们的想法是遍历所有的字符串,找到字符串的不同字符,如果不同字符的字符串中的计数小于或等于M的给定值,然后加1计数。
下面是上述方法的实现:
C++
// C++ implementation to count
// the number of strings in the
// array whose distinct characters
// is less than or equal to M
#include
#include
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 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
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 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
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)
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 set = new HashSet();
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
Javascript
输出:
2
性能分析:
- 时间复杂度: O(N * M),其中 M 是字符串的最大长度。
- 辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live