Check whether count of distinct characters in a string is Prime or not
Given a string of lowercase English alphabets. The task is to check if the count of distinct characters in the string is prime or not.
Examples:
Input : str = "geeksforgeeks" Output :Yes Explanation: The number of distinct characters in the string is 7, and 7 is a prime number. Input : str ="geeks" Output : No
In this problem first we have to count the distinct characters in the string. We will use a map to store the frequency of each alphabets. The next step is to count the number of distinct characters, and check whether the number is prime or not .
If the number is prime we will print Yes, else No.
Below is the implementation of the above approach:
C++
// C++ program to check whether count of // distinct characters in a string // is Prime or not #include <bits/stdc++.h> using namespace std; // Find whether a number is prime or not bool isPrime(int n) { int i; // 1 is not prime if (n == 1) return false; // check if there is any factor or not for (i = 2; i <= sqrt(n); i++) { if (n % i == 0) return false; } return true; } // Count the distinct characters in a string int countDistinct(string s) { // create a map to store the // frequency of characters unordered_map<char, int> m; // traverse the string for (int i = 0; i < s.length(); i++) { // increase the frequency of character m[s[i]]++; } return m.size(); } // Driver code int main() { string str = "geeksforgeeks"; if (isPrime(countDistinct(str))) cout << "Yes" << endl; else cout << "No" << endl; return 0; } |
Java
// Java program to check whether count of // distinct characters in a string // is Prime or not import java.util.*; class GFG { // Find whether a number is prime or not static boolean isPrime(int n) { int i; // 1 is not prime if (n == 1) return false; // check if there is any factor or not for (i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) return false; } return true; } // Count the distinct characters in a string static int countDistinct(String s) { // create a map to store the // frequency of characters Set<Character> m = new HashSet<Character>(); // traverse the string for (int i = 0; i < s.length(); i++) { // increase the frequency of character m.add(s.charAt(i)); } return m.size(); } // Driver code public static void main(String []args) { String str = "geeksforgeeks"; if (isPrime(countDistinct(str))) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by ihritik |
Python 3
# Python 3 program to check whether # count of distinct characters in a # string is Prime or not # from math library import # sqrt method from math import sqrt # Find whether a number # is prime or not def isPrime(n) : # 1 is not prime if n == 1 : return False # check if there is any # factor or not for i in range(2, int(sqrt(n)) + 1) : if n % i == 0 : return False return True # Count the distinct characters # in a string def countDistinct(s) : # create a dictionary to store # the frequency of characters m = {} # dictionary with keys and its # initialize with value 0 m = m.fromkeys(s, 0) # traverse the string for i in range(len(s)) : # increase the frequency # of character m[s[i]] += 1 return len(m.keys()) # Driver code if __name__ == "__main__" : str = "geeksforgeeks" if isPrime(countDistinct(str)) : print("Yes") else : print("No") # This code is contributed # by ANKITRAI1 |
C#
// C# program to check whether count of // distinct characters in a string // is Prime or not using System; using System.Collections.Generic; class GFG { // Find whether a number is prime or not static bool isPrime(int n) { int i; // 1 is not prime if (n == 1) return false; // check if there is any factor or not for (i = 2; i <= Math.Sqrt(n); i++) { if (n % i == 0) return false; } return true; } // Count the distinct characters in a string static int countDistinct(String s) { // create a map to store the // frequency of characters HashSet<char> m = new HashSet<char>(); // traverse the string for (int i = 0; i < s.Length; i++) { // increase the frequency of character m.Add(s[i]); } return m.Count; } // Driver code public static void Main(String []args) { String str = "geeksforgeeks"; if (isPrime(countDistinct(str))) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code has been contributed by 29AjayKumar |
Yes
Recommended Posts:
- Check whether the frequencies of all the characters in a string are prime or not
- Count characters in a string whose ASCII values are prime
- Find distinct characters in distinct substrings of a string
- String with k distinct characters and no same characters adjacent
- Count distinct substrings that contain some characters at most k times
- Count number of substrings with exactly k distinct characters
- Convert given string so that it holds only distinct characters
- Sort an array according to the increasing count of distinct Prime Factors
- Print all distinct characters of a string in order (3 Methods)
- Count of distinct sums that can be obtained by adding prime numbers from given arrays
- Length of the smallest sub-string consisting of maximum distinct characters
- XOR of Prime Frequencies of Characters in a String
- Sum and Product of Prime Frequencies of Characters in a String
- Program to print characters present at prime indexes in a given string
- Check if given string can be split into four distinct strings
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.



